XML Schema Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.11

Testing Examples of XSD File and XML File

This section provides an example XML file assigned with an example XML Schema file (XSD).

Note that tutorial examples given in this section were taken in 2002.

In order to test my XML Schema validator using Xerces2, I wrote this example XSD file, dictionary.xsd:

<?xml version="1.0"?>
<!-- dictionary.xsd
   Copyright (c) 2002 by Dr. Herong Yang. All rights reserved.
-->
<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>

Based on this XSD file, I wrote this invalid example XML file, dictionary_invalid_xsd.xml, with the XSD file assigned through the "xsi" attribute:

<?xml version="1.0"?>
<dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="dictionary.xsd">
<!-- dictionary_invalid_xsd.xml
   Copyright (c) 2002 by Dr. Herong Yang. 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>

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

XML Parser API - Xerces2 Java Parser

 Installing Xerces2 Java Parser

Testing Examples of XSD File and XML File

 XML Schema (XSD) Validation using XMLReader

 XML Schema (XSD) Validation using SAXParser

 XML Schema Language - Basics

 XML Schema Built-in Datatypes

 Complex Element Declaration

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2009
Testing Examples of XSD File and XML File