Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate an XML File Against Multiple Schema Definitions

Tags:

java

xerces

xsd

I'm trying to validate an XML file against a number of different schemas (apologies for the contrived example):

  • a.xsd
  • b.xsd
  • c.xsd

c.xsd in particular imports b.xsd and b.xsd imports a.xsd, using:

<xs:include schemaLocation="b.xsd"/>

I'm trying to do this via Xerces in the following manner:

XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory(); Schema schema = xmlSchemaFactory.newSchema(new StreamSource[] { new StreamSource(this.getClass().getResourceAsStream("a.xsd"), "a.xsd"),                                                          new StreamSource(this.getClass().getResourceAsStream("b.xsd"), "b.xsd"),                                                          new StreamSource(this.getClass().getResourceAsStream("c.xsd"), "c.xsd")});      Validator validator = schema.newValidator(); validator.validate(new StreamSource(new StringReader(xmlContent))); 

but this is failing to import all three of the schemas correctly resulting in cannot resolve the name 'blah' to a(n) 'group' component.

I've validated this successfully using Python, but having real problems with Java 6.0 and Xerces 2.8.1. Can anybody suggest what's going wrong here, or an easier approach to validate my XML documents?

like image 513
Jon Avatar asked Jul 07 '09 21:07

Jon


People also ask

Can we validate XML documents against a schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.

How you will validate XML document using schema?

XML documents are validated by the Create method of the XmlReader class. To validate an XML document, construct an XmlReaderSettings object that contains an XML schema definition language (XSD) schema with which to validate the XML document. The System. Xml.

Can XML have multiple schemas?

Deal with multiple XML schemas A restriction on the XML schema import is that only one XML schema can be loaded as source or target schema. If you want to load multiple XML schemas, please create a schema including or importing the XML schemas you want to combine.


2 Answers

So just in case anybody else runs into the same issue here, I needed to load a parent schema (and implicit child schemas) from a unit test - as a resource - to validate an XML String. I used the Xerces XMLSchemFactory to do this along with the Java 6 validator.

In order to load the child schema's correctly via an include I had to write a custom resource resolver. Code can be found here:

https://code.google.com/p/xmlsanity/source/browse/src/com/arc90/xmlsanity/validation/ResourceResolver.java

To use the resolver specify it on the schema factory:

xmlSchemaFactory.setResourceResolver(new ResourceResolver()); 

and it will use it to resolve your resources via the classpath (in my case from src/main/resources). Any comments are welcome on this...

like image 51
Jon Avatar answered Oct 02 '22 13:10

Jon


http://www.kdgregory.com/index.php?page=xml.parsing section 'Multiple schemas for a single document'

My solution based on that document:

URL xsdUrlA = this.getClass().getResource("a.xsd"); URL xsdUrlB = this.getClass().getResource("b.xsd"); URL xsdUrlC = this.getClass().getResource("c.xsd");  SchemaFactory schemaFactory = schemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); //--- String W3C_XSD_TOP_ELEMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"    + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\">\n"    + "<xs:include schemaLocation=\"" +xsdUrlA.getPath() +"\"/>\n"    + "<xs:include schemaLocation=\"" +xsdUrlB.getPath() +"\"/>\n"    + "<xs:include schemaLocation=\"" +xsdUrlC.getPath() +"\"/>\n"    +"</xs:schema>"; Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(W3C_XSD_TOP_ELEMENT), "xsdTop")); 
like image 20
iolha Avatar answered Oct 02 '22 14:10

iolha