Java: SAX Schema validation

Refresh

February 2019

Views

5k time

1

I've following two schema's. Master.xsd and Child.xsd

  1. Child.xsd is imported by the Master.xsd.
  2. Master file has target namespace 'pub'.
  3. Child file no such namespace.

When I try to validate xml with Master.xsd, I get error

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'author' to a(n) 'element declaration' component.

I've also tried using in master.xsd, this time i get similar error:

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'pub:author' to a(n) 'element declaration' component.

Though this get validated by XMLSpy successfully.

Here are the schema's, calling code and the validation code:

Master.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:pub="http://schema.abc.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schema.abc.com">
    <xs:import schemaLocation="Child.xsd"/>
    <xs:element name="books">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="pub:book"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="pub:published_date"/>
                <xs:element ref="author"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="published_date" type="xs:dateTime"/>
</xs:schema>

Child.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="author">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="first_name"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="first_name" type="xsd:string"/>
</xsd:schema>

Sample.xml that needs to be validated:

<?xml version="1.0" encoding="UTF-8"?>
<pub:books xsi:schemaLocation="http://schema.abc.com" xmlns:pub="http://schema.abc.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <pub:book>
        <pub:published_date>2001-12-17T09:30:47Z</pub:published_date>
        <author>
            <first_name>Adi</first_name>
        </author>
    </pub:book>
</pub:books>

Java code for validation:

private void validate(final String schema, final String xml) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        InputStream is = getClass().getResourceAsStream(schema);
        Schema schema;
        try {
            schema = schemaFactory.newSchema(new StreamSource(is));
            Validator validator = schema.newValidator();
            Source xmlSource = new StreamSource( new StringReader(xml));

            validator.validate(xmlSource);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

calling code:

validate(masterXSDPath, "xmlString");

Please tell where am I going wrong??

adi

2 answers

2

i would imagine you would want both schemas available, thus something like:

schemaFactory.newSchema(new Source[]{new StreamSource(is1), new StreamSource(is2)});

alternately, you could provide a custom LSResourceResolver to the SchemaFactory.

2

Fixed it by implementing LSResourceResolver. The Child.xsd was not found.

look here for more detials https://stackoverflow.com/a/2342859/842210

adi