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
  • 1,711
  • 3
  • 29
  • 50

2 Answers2

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.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • import statement is of no use then? – adi Mar 12 '12 at 22:55
  • 1
    @adi - the import statement is required. however, the import statement does not adequately inform the schema processor where to locate the _actual_ schema. hence, you must provide it explicitly, or provide a way to load it on the fly. – jtahlborn Mar 13 '12 at 01:36
  • the reason this works is that the is1/is2 xsd's internally say "we belong to this namespace" and those namespaces are used within the .xml file being validated, FWIW :) – rogerdpack Feb 06 '19 at 17:34
2

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

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

Community
  • 1
  • 1
adi
  • 1,711
  • 3
  • 29
  • 50