Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Resource: books.xml and books.xsd

This example uses two resources files. One is an XML data file (books.xml) and the other is the XML schema definition file (books.xsd). According to books.xsd, the second <book> element in books.xml is missing the required <pub_date> child element. Therefore, when we attempt to validate the XML file against the given schema, we should get a validation error.

XML File (books.xml)

<?xml version="1.0"?>
<x:books xmlns:x="urn:books">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>

   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</x:books>

XSD File (books.xsd)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books">

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

  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book" 
                  type="bks:BookForm" 
                  minOccurs="0" 
                  maxOccurs="unbounded"/>
      </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <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:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>

To add books.xml and books.xsd to the project

  1. Create a new C++ source file. For detailed instructions on how to do this, see Set Up My Visual C++ Project. Name the new file books.xml
  2. Copy books.xml and paste it into the source file you just created.
  3. Create another new C++ source file.
  4. Copy books.xsd and paste it into the source file you just created.
    Note   You can also copy the files into the project's main directory using Windows Explorer (or a command prompt).

Next, build and run the validateDOM project. The result should be the output shown in the following topic.