Question

My code is creating an XML document (with JDOM 1.1) which I wish to validate against an XSD file. However, according to http://www.w3.org/TR/xmlschema-1/#schema-loc, the schemaLocation property to give to the parser seems to work only with accessible URLs.

Isn't there a way to reference a local XSD file ? Is validating against an XSD which is not world-wide accessible impossible ? I don't get it...

Was it helpful?

Solution

Firstly, the schemaLocation attribute is only one way to specify the location of a schema, and in many cases it's not the best way (if you don't trust the document to be valid, why would you trust it to tell you where its schema is?). Most schema validators are likely to provide the option of giving a schema location externally, e.g. on the command line or via an API or GUI.

Secondly, the schemaLocation is a URI. It can therefore be a relative URL, for example "test.xsd" identifies a schema document sitting in the same directory as the source document.

OTHER TIPS

I would strongly encourage you to upgrade to JDOM 2.0.5. The mechanisms used in JDOM2 for Schema validation are much simpler to understand, and implement. Consider this code (taken from the JDOM2 package documentation (near the bottom) ):

 File xsdfile = new File("myschema.xsd");
 XMLReaderJDOMFactory factory = new XMLReaderXSDFactory(xsdfile);
 SAXBuilder sb = new SAXBuilder(factory);
 Document doc = sb.build(new File("file.xml"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top