You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Ma...@bundesbank.de on 2004/12/28 17:05:00 UTC

Validation based on nested schema files




Hi,

I have some difficulties while using Xerces-J-2.6.2 in order to parse the
following two XML-files:

=====

a.xml (references schema b and indirectly schema a; validation works)
      <?xml version="1.0" encoding="UTF-8"?>
      <elemA xmlns=http://www.abc.de/2003-01-01
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.abc.de/2003-01-01
b.xsd">0</elemA>

---

b.xml (references schema b; validation fails)
      <?xml version="1.0" encoding="UTF-8"?>
      <elemB xmlns=http://www.abc.de/2003-01-01
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.abc.de/2003-01-01
b.xsd">0</elemB>

=====

Nested schema files:

a.xsd:
      <?xml version="1.0" encoding="ISO-8859-1"?>
      <xs:schema targetNamespace=http://www.abc.de/2003-01-01
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified"
attributeFormDefault="unqualified">
                  <xs:element name="elemA" type="xs:string"/>
      </xs:schema>

---

b.xsd (b.xsd includes a.xsd, both schemas share the same target namespace):
      <?xml version="1.0" encoding="iso-8859-1"?>
      <xs:schema targetNamespace=http://www.abc.de/2003-01-01
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified"
attributeFormDefault="unqualified">
                  <xs:include schemaLocation="a.xsd"/>
                  <xs:element name="elemB" type="xs:string"/>
      </xs:schema>

=====

While both XML-files seem to match to the schema b (which references schema
a),
XERCES detects the following error while parsing “b.xml” (code see below):
      [Error] :3:74: cvc-elt.1: Cannot find the declaration of element
'elemB'.

Does anybody has an idea what could be wrong? I would be very grateful for
a hint.

Cheers
Martin

===

JAVA-Code:
First I create a grammarpool of both schema-files (should me more efficient
if validation is repeated very often). Afterwards I use this grammarpool
in order to validate the XML-files.

---

package de.test.xmlvalidator;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.Reader;

import org.apache.xerces.impl.Constants;
import org.apache.xerces.parsers.IntegratedParserConfiguration;
import org.apache.xerces.parsers.XMLGrammarPreparser;
import org.apache.xerces.util.SymbolTable;
import org.apache.xerces.util.XMLGrammarPoolImpl;
import org.apache.xerces.xni.grammars.XMLGrammarDescription;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParserConfiguration;

public class TestValidator {

      public static void main(String[] args) {

            try {

                  // Step 1: Create grammar
                  String GRAMMAR_POOL = Constants.XERCES_PROPERTY_PREFIX
                        + Constants.XMLGRAMMAR_POOL_PROPERTY;

                  SymbolTable sym = new SymbolTable(2039);
                  XMLGrammarPoolImpl grammarPool = new
XMLGrammarPoolImpl();

                  XMLGrammarPreparser preparser = new
XMLGrammarPreparser(sym);
                  preparser.setErrorHandler(new PreparserErrorHandler());


preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
                  preparser.setProperty(GRAMMAR_POOL, grammarPool);


preparser.setFeature("http://xml.org/sax/features/namespaces", true);

preparser.setFeature("http://xml.org/sax/features/validation", true);

preparser.setFeature("http://apache.org/xml/features/validation/schema",
true);

preparser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
 true);

                  String[] schema = {
                        "/resourcen/xml-schema/a.xsd",
                        "/resourcen/xml-schema/b.xsd",
                  };

                  for (int i = 0; i < schema.length; i++) {
                        InputStream schemaStream =

TestValidator.class.getClass().getResourceAsStream(schema[i]);
                        XMLInputSource xis = new XMLInputSource(null, null,
                                    null, schemaStream, null);

preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, xis);
                  }

                  // Step 2: Parse files based on schema grammar
                  XMLParserConfiguration mParserConfiguration = new
IntegratedParserConfiguration(sym, grammarPool);

mParserConfiguration.setFeature("http://xml.org/sax/features/namespaces",
true);

mParserConfiguration.setFeature("http://xml.org/sax/features/validation",
true);

mParserConfiguration.setFeature("http://apache.org/xml/features/validation/schema",
 true);

mParserConfiguration.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
 true);

                  // a.xml: Works properly
                  File file = new
File("C:\\Daten\\Temp\\Auswi\\Eingang\\a.xml");
                  Reader readerXml = new FileReader(file);
                  XMLInputSource xis = new XMLInputSource(null, null, null,
readerXml, null);
                  mParserConfiguration.parse(xis);

                  // b.xml: Paring fails with message
                  // "[Error] :3:74: cvc-elt.1: Cannot find the declaration
of element 'elemB'."
                  file = new
File("C:\\Daten\\Temp\\Auswi\\Eingang\\b.xml");
                  readerXml = new FileReader(file);
                  xis = new XMLInputSource(null, null, null, readerXml,
null);
                  mParserConfiguration.parse(xis);

            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}

--
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte
Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail
irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und
vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe dieser Mail oder von Teilen dieser Mail ist nicht gestattet.

Wir haben alle verkehrsüblichen Maßnahmen unternommen, um das Risiko der
Verbreitung virenbefallener Software oder E-Mails zu minimieren, dennoch
raten wir Ihnen, Ihre eigenen Virenkontrollen auf alle Anhänge an dieser
Nachricht durchzuführen. Wir schließen außer für den Fall von Vorsatz oder
grober Fahrlässigkeit die Haftung für jeglichen Verlust oder Schäden durch
virenbefallene Software oder E-Mails aus.

Jede von der Bank versendete E-Mail ist sorgfältig erstellt worden, dennoch
schließen wir die rechtliche Verbindlichkeit aus; sie kann nicht zu einer
irgendwie gearteten Verpflichtung zu Lasten der Bank ausgelegt werden.
______________________________________________________________________

This e-mail may contain confidential and/or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorised copying, disclosure or distribution of  the material in this
e-mail or of parts hereof is strictly forbidden.

We have taken precautions to minimize the risk of transmitting software
viruses but nevertheless advise you to carry out your own virus checks on
any attachment of this message. We accept no liability for loss or damage
caused by software viruses except in case of gross negligence or willful
behaviour.

Any e-mail messages from the Bank are sent in good faith, but shall not be
binding or construed as constituting any kind of obligation on the part of
the Bank.