You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Michael Nielsen (Info)" <mi...@dsb.dk> on 2002/09/24 08:47:03 UTC
How to use a schema and dtd at the same time?
Hi
Im using the latest version of xerces and using DOM to construct a DOM
document.
I have this problem using both the specified DTD and xml schema for
validation at the same time.
The thing is that the xml document contains special html entities that needs
to be resolved, otherwise the parser compliants.
Here the exception I get:
java.lang.Exception: XML-dokumentet kan ikke valideres:
org.xml.sax.SAXParseException: Element type "asset" must be
declared.org.xml.sax.SAXParseException: Element type "alias" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "text" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "xhtml" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "text" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "datetime" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "enumeration" must be
declared.org.xml.sax.SAXParseException: Element type "text" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "text" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "text" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "enumeration" must be
declared.org.xml.sax.SAXParseException: Element type "text" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "enumeration" must be
declared.org.xml.sax.SAXParseException: Element type "text" must be
declared.org.xml.sax.SAXParseException: Element type "field" must be
declared.org.xml.sax.SAXParseException: Element type "association" must be
declared.org.xml.sax.SAXParseException: Element type "asset" must be
declared.org.xml.sax.SAXParseException: Element type "alias" must be
declared.
Here is a snippet from the XML document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE asset PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN"
"http://legolas.dsb.dk:4520/mille/integration/xsd/xhtml-lat1.ent">
<asset category="g" description="Pressemeddelelse 15.05.1997#10"
method="create" name="Pressemeddelelse 15.05.1997#10"
type="Pressemeddelelse"
xmlns:html="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://legolas.dsb.dk:4520/futuretense_cs/Sam
ples/integration/indholdstype.xsd">
.....
</asset>
Note the dtd decleration in the <!DOCTYPE ...> element, that should resolve
the special html entities.
And here parsing mechanism for the DOM parser constructed (code snippet):
The args are the xml doc. as a String object and the arg. specifying if
validation should be done or not. (I ofcause has this arg as true).
public static Document parse(String source, boolean mustValidate) throws
Exception {
DOMParser parser = null;
ParseErrorHandler handler = new ParseErrorHandler();
try {
parser = new org.apache.xerces.parsers.DOMParser();
parser.setFeature("http://xml.org/sax/features/validation",
mustValidate );
parser.setFeature("http://apache.org/xml/features/validation/schema",
mustValidate);
parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whit
espace", false);
parser.setErrorHandler(handler);
StringReader reader = new StringReader(source);
parser.parse(new InputSource(reader));
if(!handler.valid) {
throw new Exception("XML-dokumentet kan ikke valideres: " +
handler.msg);
}
} catch (SAXException e) {
e.printStackTrace();
throw new Exception("XML-dokumentet kan ikke valideres: " + e);
} catch (IOException e) {
e.printStackTrace();
throw new Exception("XML-dokumentet kan ikke valideres: " + e);
}
Document doc = parser.getDocument();
doc.normalize();
return doc;
}
Can anybody please help! Its probable a matter of setting the right features
or something. If anybody has expirienced anything similar please let me
know!
Thanx!
Regards from
Michael Nielsen