Example 2: Adding a Namespace to the Schema

MSXML 5.0 SDK

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

Example 2: Adding a Namespace to the Schema

In the last example, we allowed the schemas to be applied using the default namespace. This was done in order to keep it simple and to contrast clearly what differentiates using inline schemas from using external schemas.

But what if you need to incorporate a schema that validates to another namespace? In this example, we will look at the changes needed to modify the files you created in Example 1 so that they are namespace aware. For the purposes of this example, we will determine that the <book> element needs to exist in a separate "x" namespace instead.

File: inline-namespaced.xml

The following is an altered version of the use-inline.xml file you created in Example 1. The changes are highlighted in bold.

<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         xmlns:x="urn:book"> 
<!-- START OF SCHEMA -->
<xsd:schema targetNamespace="urn:book">
 <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 -->
   <x: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>
   </x:book>
</catalog>

File: external-namespaced.xml

The following is an altered version of the use-external.xml file you created in Example 1. The changes are highlighted in bold.

<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:x="urn:book">
   <x:book xsi:schemaLocation="book books2.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>
   </x:book>
</catalog>

File: books2.xsd

<?xml version="1.0"?>
<xsd:schema targetNamespace="urn:book">
 <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: validate2.vbs

The following is an altered version of the validate.vbs file you created in Example 1. The changes are highlighted in bold.

  '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, "inline-namespaced.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 "SelectionNamespaces", "xmlns:x='urn:book'"
     xmldom.setProperty "SelectionLanguage", "XPath"
     Set node = xmldom.selectSingleNode("//x:book")
     MsgBox "Validation successful" & vbCrLf & _
            "=====================" & vbCrLf & node.xml
  End If

Try It!

  1. Copy the namespaced inline version of the sample XML file above and paste it into a text file. Save the file as inline-namespaced.xml.
  2. Copy the namespaced external version of the sample XML file from above. Paste it into a text file, and save the file as external-namespaced.xml in the same directory where you saved inline-namespace.xml.
  3. Copy the XSD schema for the <book> element from above. Paste it into a text file, and save the file as books2.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 validate2.vbs. Use the same directory as you did in the previous steps.
  5. Double-click validate2.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 (inline-namespaced.xml).
  7. Repeat the previous step, but substitute the version with the external XSD schema (external-namespaced.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 1: Comparing an Inline Schema to an External Schema