XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
Running XMLReaderValidator on XSD 1.1 Schema
This section provides a tutorial example on new built-in datatypes introduced in XSD 1.1 and on validating XSD 1.1 schema with the XMLReader class from Xerces2 JAR files.
The previous tutorial showed us how to use the org.apache.xerces.parsers.SAXParser class in Xerces2 JAR files to validate XML document again XSD document. But the XSD document used, dictionary.xsd, does not use any XSD 1.1 features.
In order to test XSD 1.1 features with Xerces2 JAR files, I wrote this sample XSD document, xsd11_datatype_test, that uses 4 built-in datatypes introduced in XSD 1.1:
<?xml version="1.1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- xsd11_datatype_test.xsd - Copyright (c) 2013 HerongYang.com. All Rights Reserved. --> <xs:element name="XSD11_Datatype_Test"> <xs:complexType> <xs:sequence> <xs:element name="AnyAtomicType" type="xs:anyAtomicType" maxOccurs="unbounded"/> <xs:element name="DateTimeStamp" type="xs:dateTimeStamp" maxOccurs="unbounded"/> <xs:element name="DayTimeDuration" type="xs:dayTimeDuration" maxOccurs="unbounded"/> <xs:element name="YearMonthDuration" type="xs:yearMonthDuration" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Here is a sample XML document to test this XSD document:
<?xml version="1.1"?> <!-- xsd11_datatype_test.xml - Copyright (c) 2013 HerongYang.com. All Rights Reserved. --> <XSD11_Datatype_Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xsd11_datatype_test.xsd"> <!-- 3 valid "anyAtomicType" representations --> <AnyAtomicType> 1971-05-16T00:00:01 </AnyAtomicType> <AnyAtomicType> 头 </AnyAtomicType> <AnyAtomicType><![CDATA[ Hello <b>Herong</b>! ]]> </AnyAtomicType> <!-- 1 invalid "anyAtomicType" representations --> <AnyAtomicType> Hello <b size="9">Herong</b>! </AnyAtomicType> <!-- 1 valid "dateTimeStamp" representations --> <DateTimeStamp> 1971-05-16T00:00:01Z </DateTimeStamp> <!-- 1 invalid "dateTimeStamp" representations --> <DateTimeStamp> 头 </DateTimeStamp> <!-- 1 valid "dayTimeDuration" representations --> <DayTimeDuration> P3DT5H20M30.123S </DayTimeDuration> <!-- 1 invalid "dayTimeDuration" representations --> <DayTimeDuration> P1Y2M3DT5H20M30.123S </DayTimeDuration> <!-- 1 valid "yearMonthDuration" representations --> <YearMonthDuration> P1Y2M </YearMonthDuration> <!-- 1 invalid "yearMonthDuration" representations --> <YearMonthDuration> P1Y2M3DT5H20M30.123S </YearMonthDuration> </XSD11_Datatype_Test>
When running XMLReaderValidator.java with JDK 13 and Xerces2 2.12.1 (XML Schema 1.1), I get this error:
herong> java_xerces XMLReaderValidator.java xsd11_datatype_test.xml Error: Public ID: null System ID: file:///C:/herong/xsd11_datatype_test.xsd Line number: 10 Column number: 29 Message: src-resolve.4.2: Error resolving component 'xs:anyAtomicType'. It was detected that 'xs:anyAtomicType' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:///C:/herong/xsd11_datatype_test.xsd'. If this is the incorrect namespace, perhaps the prefix of 'xs:anyAtomicType' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///C:/herong/xsd11_datatype_test.xsd'. Error: Public ID: null System ID: file:///C:/herong/xsd11_datatype_test.xsd Line number: 10 Column number: 29 Message: src-resolve: Cannot resolve the name 'xs:anyAtomicType' to a(n) 'type definition' component.
I am confused with this error. What does it mean that 'xs:anyAtomicType' is in the namespace, but components are not referenceable? See next tutorial for the answer.
Anyway, the way I am using the org.apache.xerces.parsers.SAXParser class supports XSD 1.0 only. It does not support XSD 1.1.
Table of Contents
XML Editor and Schema Processor - XMLPad
Java API for XML Processing - JAXP
JAXP - XML Schema (XSD) Validation
Xerces2 Java Parser - Java API of XML Parsers
JAXP, DOM and SAX APIs in Xerces2 JARs
XML Schema (XSD) Validation using XMLReader
►Running XMLReaderValidator on XSD 1.1 Schema
XML Schema (XSD) Validation using SAXParser
SAXParser for XSD Validation Fixed
SAXParser for XSD 1.1 Validation
Xsd11SchemaValidator.java for XSD 1.1 Validation
Xsd11SchemaValidator.java XSD 1.1 Test Result
Introduction of XSD Built-in Datatypes
"string" and Its Derived Datatypes
"decimal" and Its Derived Datatypes
"dateTime" and Its Related Datatypes
Miscellaneous Built-in Datatypes
Facets, Constraining Facets and Restriction Datatypes
"simpleType" - Defining Your Own Simple Datatypes
Identity-Constraints: unique, key and keyref
Assertion as Custom Validation Rules
XML Schema Location and Namespace in XML Documents
Overriding Element Types in XML Documents