Sample XML and XSD Files (SAX Validator)


[This sample application uses a feature that was first implemented in MSXML 4.0.]

This example attempts to validate an XML data file (books.xml) against an XML schema definition file (books.xsd). According to books.xsd, each <book> element must have a <pub_date> child element. The second <book> element in books.xml does not have this required element. Therefore, when we attempt to validate the XML file , we should get a validation error.

To create books.xsd and books.xml

  1. Open Notepad.

  2. Copy the code for books.xsdand paste it into Notepad.

  3. From Notepad, save the file as books.xsd to the same folder where you are creating the SAX Validator application.

  4. Copy the code for books.xml and paste it in Notepad.

  5. Save the file as books.xml to the same folder where you are creating the SAX Validator application.

XML File (books.xml)

<?xml version="1.0"?>
<x:books xmlns:x="urn:books">
   <book id="bk001">
      <title>The First Book</title>
      <review>An amazing story of nothing.</review>

   <book id="bk002">
      <title>The Poet's First Poem</title>
      <review>Least poetic poems.</review>

XSD File (books.xsd)

<xsd:schema xmlns:xsd=""

  <xsd:element name="books" type="bks:BooksForm"/>

  <xsd:complexType name="BooksForm">
      <xsd:element name="book" 

  <xsd:complexType name="BookForm">
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    <xsd:attribute name="id"   type="xsd:string"/>

See Also

Validate Documents Using SAX
Overview of the SAX Validator Application
Application Form (SAX Validator)
MyValidator Class (SAX Validator)
Run the Application (SAX Validator)
How the SAX Validator Application Works