simonalexander2005
simonalexander2005

Reputation: 4577

Java XSD validation depends on order XSDs are provided

Given two XSD schema files:

a.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="b.xsd"/>
    <xs:element name="TestMessage">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="TestSubItem"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

b.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TestSubItem">
        <xs:complexType>
            <xs:attribute name="name" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="test"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
</xs:schema>

The following java code successfully validates against the schemas:

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.commons.io.FileUtils;
import org.xml.sax.SAXException;

try
{
    String xsdDirectory = "C:\\XSDFiles";
    String xmlString = "<TestMessage xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <TestSubItem name=\"test\"/> </TestMessage>";

     Collection<File> xsdFiles = FileUtils.listFiles(new File(xsdDirectory), new String[] { "xsd", "XSD" }, false);
    List<Source> sourceList = new ArrayList<>();
    for (File xsdFile: xsdFiles) {
        sourceList.add(new StreamSource(xsdFile));
    }
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    // 0 then 1, so add a.xsd then b.xsd (alphabetical)
    Schema schema = factory.newSchema(new Source[] {sourceList.get(0), sourceList.get(1)});
    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(new StringReader(xmlString)));
    System.out.println("valid");
}
catch (SAXException e)
{
    System.out.println(e.toString());
}

But, if you swap around the order the schemas are added to the Source array, so that the XSDs are added in the reverse order (b before a):

Schema schema = factory.newSchema(new Source[] {sourceList.get(1), sourceList.get(0)});

then you get the following exception:

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 68; cvc-elt.1.a: Cannot find the declaration of element 'TestMessage'.

This is fine here, because it's an arbitrary example - but if I'm loading the files from a directory and don't know the "correct sort order", then how can I get the code to look for the root element across all files and not just the first one it finds?

Upvotes: 1

Views: 647

Answers (2)

M123
M123

Reputation: 11

This way you can instruct schemafactory to go through all imports. Can change depending on underlying implementation.

        SchemaFactory factory = schemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        factory.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);

Upvotes: 1

Andreas
Andreas

Reputation: 159165

Javadoc of newSchema(Source[] schemas):

parsers may choose to ignore all but the first <import> for a given namespace

Read the full description in the javadoc for more context information.

So only the first file listed is used.

Since a.xsd has an <xs:include> for b.xsd, they both get loaded when a.xsd is first.

If b.xsd is first, then only b.xsd is loaded, which means that <xs:element name="TestMessage"> is missing, and hence the error.

Upvotes: 3

Related Questions