Herong's Tutorial Notes On XML Technologies
Dr. Herong Yang, Version 3.04

XSD Validation in Java

Part:   1  2   3 

(Continued from previous part...)

Here is the linked XSD file, dictionary.xsd:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="dictionary" type="dictionaryType"/>
 <xsd:complexType name="dictionaryType">
  <xsd:sequence>
   <xsd:element name="word" type="wordType" maxOccurs="unbounded"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="wordType">
  <xsd:sequence>
   <xsd:element name="name" type="xsd:string"/>
   <xsd:element name="definition" type="definitionType" 
    maxOccurs="unbounded"/>
   <xsd:element name="update" type="updateType" minOccurs="0"/>
  </xsd:sequence>
  <xsd:attribute name="acronym" type="xsd:boolean" use="optional"/>
  <xsd:attribute name="symbol" type="xsd:boolean" use="optional"/>
 </xsd:complexType>
 <xsd:complexType name="definitionType" mixed="true">
  <xsd:attribute name="reference" type="xsd:string"/>
 </xsd:complexType>
 <xsd:complexType name="updateType">
  <xsd:attribute name="date">
   <xsd:simpleType>
    <xsd:restriction base="xsd:string">
     <xsd:pattern value="\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}"/>
    </xsd:restriction>
   </xsd:simpleType>
  </xsd:attribute>
 </xsd:complexType>
</xsd:schema>

Now run the XMLReaderValidator program:

java -cp .;\local\xerces-2_3_0\xercesImpl.jar dictrionary_invalid_xsd.xml

you will get:

Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-datatype-valid.1.2.1: 'yes' is not a valid 'boolean' 
value.
Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-attribute.3: The value 'yes' of attribute 'acronym' on 
element 'word' is not valid with respect to its type.
Error:
   Public ID: null
   System ID: file:///D:/herong/src/dictionary_invalid_xsd.xml
   Line number: 11
   Column number: 31
   Message: cvc-pattern-valid: Value '23-Dec-2003' is not facet-valid 
with respect to pattern '\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}'.
Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 11
   Column number: 31
   Message: cvc-attribute.3: The value '23-Dec-2003' of attribute 'date'
on element 'update' is not valid with respect to its type.
Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 20
   Column number: 33
   Message: cvc-complex-type.3.2.2: Attribute 'editor' is not allowed to
appear in element 'update'.
Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 22
   Column number: 36
   Message: cvc-datatype-valid.1.2.1: 'no' is not a valid 'boolean' 
value.
Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 22
   Column number: 36
   Message: cvc-attribute.3: The value 'no' of attribute 'symbol' on 
element 'word' is not valid with respect to its type.

This is perfect. It tells you where the error is, and why it's an error.

(Continued on next part...)

Part:   1  2   3 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - XSD Validation in Java