I am using the DOM level 3 api to build a Dom from an instance XML file without a DocType (DTD). After I have built the Dom I want to revalidate it against an external DTD choosen by the user.
But no validation takes place.
On the other hand: If the xml has a reference to an external DTD, this DTD will be used, no matter what the user assigns as the new DTD.
And If the XML instance had an internal DocType subset, this will be used, discarding the assigned DTD.

I want the choosen DTD's to be used in the validation. Ok, I could use a entity resolver or catalog, but I think it should work in this simple way. In fact, if i am working with xsd files, all works like expected.
Has some one an idea how to solve this?


Here is my code.
First parse the xml without validation into Dom:

System.setProperty(
DOMImplementationRegistry.PROPERTY,
"org.apache.xerces.dom.DOMImplementationSourceImpl");
DOMImplementationRegistry registry =
DOMImplementationRegistry.newInstance();

DOMImplementationLS impl =
(DOMImplementationLS) registry.getDOMImplementation("LS");
System.out.println("impl " + impl);

LSParser parser =
impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
config.setParameter("datatype-normalization", Boolean.FALSE);
config.setParameter("error-handler", this);
config.setParameter("validate", Boolean.FALSE);
config.setParameter("validate-if-schema", Boolean.FALSE);
config.setParameter("psvi", Boolean.TRUE);

Exception throwable = null;
Document document = null;

try
{
document = parser.parse(input);
}
catch (Exception e)
{
throwable = e;
System.out.println(e);
}

Afterwards I am trying to revalidate the Dom document, after the user has choosen an external DTD
I retrieve the DomConfiguration from the document:
DocumentImpl docImpl = (DocumentImpl) document;
DOMConfiguration config = docImpl.getDomConfig();

And I set the following parameters on the domconfiguration.(schema contains the absolute path to the choosen schema file):

//set error handler
config.setParameter("error-handler", this);

// set validation feature
config.setParameter("validate", Boolean.TRUE);

// set schema language
if (source.getSchemaType() == XMLSource.SCHEMA_TYPE_XSD)
{
System.out.println(this.getClass() + " Use schemaGrammar");
config.setParameter(
"schema-type",
"http://www.w3.org/2001/XMLSchema");
}
else
{
System.out.println(this.getClass() + " Use dtdGrammar");
config.setParameter(
"schema-type",
"http://www.w3.org/TR/REC-xml");

}

config.setParameter("datatype-normalization", Boolean.FALSE);
config.setParameter("psvi", Boolean.TRUE);
config.setParameter("schema-location", schema);

then I revalidate the Document
((DocumentImpl) document).normalizeDocument();



Reply via email to