Skip to main content

Can't validate xml with DTDs

4 replies [Last post]
Joined: 2007-05-14

I'm trying to validate some xml files (not created by me). Whenever the xml has a DTD but no schemaLocation or no-namespace schema location specified, the validator (both xerces 2.9 and Java 1.5's default) says

"cvc-elt.1: Cannot find the declaration of element "

The xml is valid and the external DTDs were resolved, but the validator doesn't use the element definitions from the DTD.

What do I need to do to get the validator to accept my xml files?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-12-15

I think you might need to configure the parser factory to be namespace-aware, i.e. factory.setNamespaceAware(true); If that doesn't work, could you paste your code and the error stacktrace here?

Joined: 2007-05-14

Thanks for the reply.

I'm not able to set the factory to be namespace aware (there is no method and no recognised features). Here's the code + stack traces. This is really puzzling me.

System.setProperty("javax.xml.validation.SchemaFactory:", "org.apache.xerces.jaxp.validation.XMLSchemaFactory");

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

LSResourceResolver resolver = new SchemaResolver( );

Schema schema = factory.newSchema();

Validator validator = schema.newValidator();

InputSource inputSource = new InputSource(new FileInputStream(fileName));
Source source = new SAXSource(inputSource);

} catch (...

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'toc'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElementAfterName(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorHandlerImpl.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(
at com.package.validation.xml.ValidateXML.isValid(

Joined: 2004-12-15

The schema validator can not be used to validate xml using DTDs. If I understand correctly, you have files that reference to either DTDs or schema, you'd have to process them separately. For the ones with DTDs, you could create a SAX parser and setValidating to true on the parser factory to validate them.

Joined: 2007-05-14

Thanks for the answer - it makes so much sense. I'm writing an automated xml validator and I expect files with both DTDs or schemas. I'll follow your advice and parse with validating true.

Many thanks, again