JDK Tutorials - Herong's Tutorial Examples - Version 6.00, by Dr. Herong Yang

XML XSD Schema Validation Test Result

This section provides a tutorial example on how to run the XML schema validation program, XMLReaderValidator.java. The result provides errors with detailed locations and descriptions.

In order to test my XML schema validator, XMLReaderValidator2.java, I prepared the following XML file, dictionary_invalid_xsd.xml:

<?xml version="1.0"?>
<dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="dictionary.xsd">
<!-- dictionary_invalid_xsd.xml
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
 <word acronym="yes">
  <name>XML</name>
  <definition reference="Herong's Notes">eXtensible Markup 
Language.</definition>
  <update date="23-Dec-2003"/>
 </word>
 <word symbol="true">
  <name><</name>
  <definition>Mathematical symbol representing the "less than" logical 
operation, like: 1<2.</definition>
  <definition>Reserved symbol in XML representing the beginning of 
tags, like: <![CDATA[<p>Hello world!</p>]]>
  </definition>
  <update editor="Herong Yang"/>
 </word>
 <word symbol="no" acronym="false">
  <name>extensible</name>
  <definition>Capable of being extended.</definition>
 </word>
</dictionary>

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 with JDK 1.8:

>java XMLReaderValidator2 dictrionary_invalid_xsd.xml

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-datatype-valid.1.2.1: 'yes' is not a valid value for 
      'boolean'.

Error:
   Public ID: null
   System ID: file:///C:/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, 
      'boolean'.

Error:
   Public ID: null
   System ID: file:///C:/herong/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}' for type
      '#AnonType_dateupdateType'.

Error:
   Public ID: null
   System ID: file:///C:/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, 'null'.

Fattal error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 14
   Column number: 10
   Message: The content of elements must consist of well-formed 
      character data or markup.
org.xml.sax.SAXParseException; systemId: 
   file:///C:/herong/dictionary_invalid_xsd.xml; lineNumber: 14; 
   columnNumber: 10; The content of elements must consist of well-
   formed character data or markup.

This is perfect. It provides a list of errors with exact error locations and full error descriptions.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

XSD (XML Schema Definition) - XML Validation

 XML XSD Schema Validation

XML XSD Schema Validation Test Result

 XML XSD Schema Validation with Xerces

 XSD Validation Failed with SAXParserFactory

 XSD Schema Validator on XML SAX Object

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 PDF Printing Version

XML XSD Schema Validation Test Result - Updated in 2014, by Dr. Herong Yang