XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
dom.Counter Validating XML with Associated XSD
This section provides a tutorial example on how to use the Xerces2 sample program called dom.Counter to validate XML documents with associated XSD schema files using the '-v -s' options.
To fully testing the XSD schema validation feature of dom.Counter, I need to create two files: hello.xsd and hello_xsd_invalid.xml:
herong> type hello.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="p" type="xsd:string"/> </xsd:schema> herong> type hello_xsd_invalid.xml <?xml version="1.0"?> <p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hello.xsd"> Hello <i>world</i>!</p>
Now run dom.Counter again with the "-s" option on hello_xsd_invalid.xml:
herong> java_xerces dom.Counter -s hello_xsd_invalid.xml hello_xsd_invalid.xml: 251;6;0 ms (2 elems, 2 attrs, 0 spaces, 13 chars)
Surprise, where is the expected schema validation error? The sub element "i" is not defined in hello.xsd.
Checking the documentation of Xerces2 features again at http://xerces.apache.org/xerces2-j/features.html again, I see that the schema validation feature (-s option) requires the basic validation feature (-v option) turned on to report errors:
herong> java_xerces dom.Counter -v -s hello_xsd_invalid.xml [Error] hello_xsd_invalid.xml:4:24: cvc-type.3.1.2: Element 'p' is a ^^^ simple type, so it must have no element information item ^^^ [children]. hello_xsd_invalid.xml: 273;8;0 ms (2 elems, 2 attrs, 0 spaces, ^^^ 13 chars)
Very good. dom.Counter does support XSD schema validation and report back errors, if you use "-v -s" options.
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
Installing Xerces2 Java Parser for XSD 1.1
Verify Installation of Xerces2
Xerces2 Sample Program dom.Counter
►dom.Counter Validating XML with Associated XSD
dom.GetElementsByTagName and dom.Writer
sax.DocumentTracer and sax.Writer
Examples of XSD and XML Files with Errors
sax.Writer Reporting Errors Embedded in XML Structure
XSD 1.1 not Supported by sax.Writer
XSD 1.1 Supported by jaxp.SourceValidator
Examples of XSD 1.1 and XML Files with Errors
jaxp.TypeInfoWriter as an XSD 1.1 Validation Tool
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