|
| | | | How do I validate against XML schema? | | | | |
| |
XML Schema 1.0 validation has been integrated with the
regular SAXParser and DOMParser classes, and also with the JAXP
validation API using the XSD 1.0 Schema factory. No special classes are
required to parse documents that use a schema.
For XML Schema 1.1 validation, the preferred way is to use the JAXP
validation API, using the XSD 1.1 Schema factory. Here's an example:
| | | | 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;
...
StreamSource[] schemaDocuments = /* created by your application */;
Source instanceDocument = /* created by your application */;
SchemaFactory sf = SchemaFactory.newInstance(
"http://www.w3.org/XML/XMLSchema/v1.1");
Schema s = sf.newSchema(schemaDocuments);
Validator v = s.newValidator();
v.validate(instanceDocument);
| | | | |
Similar to XML Schema 1.0 validation using SAXParser and DOMParser classes that was
available earlier with Xerces, the same has been enhanced to support XML Schema 1.1
validation as well. To be able to do this, we need to construct the XSD 1.1 Schema factory
with the following java statement, SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1")
and do the subsequent validation.
You can also refer to the JAXP sample, SourceValidator, where you
can validate XML documents against 1.1 schemas by specifying an option "-xsd11"
when running the sample.
Each document that uses XML Schema grammars must specify the location of the
grammars it uses by using an xsi:schemaLocation attribute if they use
namespaces, and an xsi:noNamespaceSchemaLocation attribute
otherwise. These are usually placed on the root / top-level element
in the document, though they may occur on any element; for more details see XML Schema Part 1 section 4.3.2.
Here is an example with no target namespace:
| | | | <document
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='document.xsd'>
...
</document> | | | | |
Here is an example with a target namespace. Note that it is an
error to specify a different namespace than the target namespace
defined in the Schema.
| | | | <document
xmlns='http://my.com'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://my.com document.xsd'>
...
</document> | | | | |
Review the sample file, 'data/personal.xsd' for an example of an XML
Schema grammar.
|
| | | | How an XPath 2.0 engine is used for XML Schema 1.1 assertions and CTAs? | | | | |
| |
XML Schema 1.1 assertions and CTAs require an XPath processor during evaluation. For XSD 1.1 assertions,
full XPath 2.0 support is required. For XSD 1.1 CTAs the XSD schema engines can
provide full XPath 2.0 support, or they can implement a smaller XPath subset
as defined by the XSD 1.1 language. For CTAs Xerces uses the XSD 1.1 CTA XPath subset language by default, but can be made to use the
full XPath 2.0 support by setting the value of Xerces feature http://apache.org/xml/features/validation/cta-full-xpath-checking to 'true'.
The native CTA XPath processor in Xerces-J was written for efficiency, so you will likely get better performance if your
XPath expressions fall within the minimum subset defined by the XML Schema 1.1 specification. For full XPath 2.0 evaluation
(for XSD 1.1 assertions, and optionally for CTAs), Xerces-J uses an XPath 2.0 engine.
Xerces-J does bundle along an XPath 2.0 engine jar for these features (that requires JDK 1.4 or later).
We would like to acknowledge, work of following people for the XPath 2.0 support that comes with Xerces's
XML Schema 1.1 processor: Andrea Bittau (responsible for the original design and implementation, of XPath 2.0
processor that's been used by Xerces. Andrea donated his XPath 2.0 processor to the Eclipse Foundation, that
Xerces uses), Dave Carver (Dave helped to set up software infrastructure at Eclipse Foundation's Web Tools Platform project,
for testing the XPath 2.0 processor as per W3C XPath 2.0 test suite. He also helped to improve compliance of
XPath 2.0 processor, to the W3C XPath 2.0 test suite by providing numerous bug fixes and implementation),
Jesper Steen Moeller (Jesper as an Eclipse's Web Tools Platform committer, helped to improve the implementation
of the XPath 2.0 processor).
|
| | | | How to specify a user defined error message, when an XML Schema 1.1 assertion returns a 'false' result? | | | | |
| |
When evaluation of an XSD 1.1 assertion fails the Xerces XML Schema validator would produce a default error message,
which would say that which element or attribute was not validated successfully by an assertion and the schema type involved
during validation. It is however possible to, specify an user-defined error message for assertion failures. This is done by
specifying an attribute "message" in the XML namespace 'http://xerces.apache.org' on an
xs:assert or xs:assertion element in the schema. In this case, the value of attribute "message"
is a text of user-defined assertion error message which is generated by the Xerces XML Schema validator when an assertion fails.
While using xs:assertion facet within simple types, if a user defined error message is used, a user may
dynamically construct the assertion error message by getting the value of XPath 2.0 context variable
$value into the error message. Following is an example of such an assertion error message:
xerces:message="The number {$value} is not divisible by 2" . With such a description of assertion error message,
a value from XML instance document is assigned to a variable reference {$value} which is a defined keyword.
During a validation failure, an error message for this example would be produced as follows,
"The number 3 is not divisible by 2" (the variable reference {$value} is assigned a value 3).
|
User-defined error messages for assertion failures are not a standard feature of the XML Schema 1.1 specification, and
are a Xerces extension to help XML Schema document authors to specify a problem domain specific error messages.
|
|
|