Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Schemas

Example 1: Comparing an Inline to an External Schema

To get started using inline schemas, we will look at an abridged version of the sample XML file (books.xml) (use-inline.xml). This file has been modified to include an inline schema, which will be used to validate the contents of any <book> element nodes that appear in the non-schema portion of the XML document. The inline schema is nested within an <xsd:schema> element.

Note   By design, inline schemas are never applied to the root element when validating document contents.

File: use-inline.xml

<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- START OF SCHEMA -->
<xsd:schema>
 <xsd:element name="book">
  <xsd:complexType>
    <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="publish_date" type="xsd:date"/>
      <xsd:element name="description" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>
<!-- END OF SCHEMA -->
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with
      XML.</description>
   </book>
</catalog>

The inline schema version of the sample document is treated exactly the same as the external schema version of the same file (use-external.xml) provided below. In the external version, the only differences are the following:

File: use-external.xml

<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <book xsi:schemaLocation="book books1.xsd" id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with
      XML.</description>
   </book>
</catalog>

File: books1.xsd

<?xml version="1.0"?>
<xsd:schema>
 <xsd:element name="book">
  <xsd:complexType>
    <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="publish_date" type="xsd:date"/>
      <xsd:element name="description" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>

File: validate.vbs

  'Create an XML DOMDocument object.
  Dim xmldom, sXmlFile
  Set xmldom = CreateObject("MSXML2.DOMDocument.5.0")

  'Get the name of the file to validate as user input.
  sXmlFile = InputBox("Enter the name of the file to validate:", _
             Wscript.ScriptName, "use-inline.xml")

  'Load and validate the specified file into the DOM.
  xmldom.async = False
  xmldom.validateOnParse = True
  xmldom.load sXmlFile

  'Return validation results in message to the user.
  If xmldom.parseError.errorCode <> 0 Then
     MsgBox xmldom.parseError.errorCode & " " & _
     xmldom.parseError.reason
  Else
     Dim node
     xmldom.setProperty "SelectionLanguage", "XPath"
     Set node = xmldom.selectSingleNode("//book")
     MsgBox "Validation successful" & vbCrLf & _
            "=====================" & vbCrLf & node.xml
  End If

Try It!

  1. Copy the inline schema version of the sample XML file above and paste it into a text file. Save the file as use-inline.xml.
  2. Copy the external schema version of the sample XML file from above. Paste it into a text file, and save the file as use-external.xml in the same directory where you saved use-inline.xml.
  3. Copy the XSD schema for the <book> element from above. Paste it into a text file, and save the file as books1.xsd in the same directory where you saved the file in the previous steps.
  4. Copy the VBScript code above. Paste it into a text file, and save the file as validate.vbs. Use the same directory as you did in the previous steps.
  5. Double-click validate.vbs.

    An input box will appear that allows you to specify the name of the file to validate.

  6. Click OK to validate the default file (use-inline.xml).
  7. Repeat the previous step but substitute the version with the external XSD schema (use-external.xml) instead.

    Both the inline and external versions of the XML sample file should validate successfully and display the same output.

See Also

Using Inline Schemas (XSD) | Example 2: Adding a Namespace to the Schema