Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating XML Schema from URL works but from Local File fails?

I need to validate XML Schema Instance (XSD) documents which are programmatically generated so I'm using the following Java snippet, which works fine:

SchemaFactory factory = SchemaFactory.newInstance(
    XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema xsdSchema = factory.newSchema( // Reads URL every time...
    new URL("http://www.w3.org/2001/XMLSchema.xsd"));
Validator xsdValidator = xsdSchema.newValidator();
xsdValidator.validate(new StreamSource(schemaInstanceStream));

However, when I save the XML Schema definition file locally and refer to it this way:

Schema schema = factory.newSchema(
    new File("test/xsd/XMLSchema.xsd"));

It fails with the following exception:

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/Users/foo/bar/test/xsd/XMLSchema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

I've ensured that the file exists and is readable by doing exists() and canRead() assertions on the File object. I've also downloaded the file with a couple different utilities (web browser, wget) to ensure that there is no corruption.

Any idea why I can validate XSD instance documents when I generate the schema from the HTTP URL but I get the above exception when trying to generate from a local file with the same contents?

[Edit]

To elaborate, I've tried multiple forms of factory.newSchema(...) using Readers and InputStreams (instead of the File directly) and still get exactly the same error. Moreover, I've dumped the file contents before using it or the various input streams to ensure it's the right one. Quite vexing.

Full Answer

It turns out that there are three additional files referenced by XML Schema which must be also stored locally and XMLSchema.xsd contains an import statement whose schemaLocation attribute must be changed. Here are the files that must be saved in the same directory:

  1. XMLSchema.xsd - change schemaLocation to "xml.xsd" in the "import" element for XML Namespace.
  2. XMLSchema.dtd - as is.
  3. datatypes.dtd - as is.
  4. xml.xsd - as is.

Thanks to @Blaise Doughan and @Tomasz Nurkiewicz for their hints.

like image 584
maerics Avatar asked Aug 30 '11 20:08

maerics


2 Answers

I assume you are trying to load XMLSchema.xsd. Please also download XMLSchema.dtd and datatypes.dtd and put them in the same directory. This should push you a little bit further.

like image 195
Tomasz Nurkiewicz Avatar answered Sep 20 '22 10:09

Tomasz Nurkiewicz


UPDATE

Is XMLSchema.xsd importing any other schemas by relative paths that are not on the local file systen?


Your relative path may not be correct wrt your working directory. Try entering a fully qualified path to eliminate the possibility that the file can not be found.

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/Users/foo/bar/test/xsd/XMLSchema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .

like image 29
bdoughan Avatar answered Sep 18 '22 10:09

bdoughan