문제

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...

도움이 되었습니까?

해결책

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.

다른 팁

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"));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top