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.
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>
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:bookxsi:schemaLocation="bookbooks2.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>
<?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>
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!
<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.An input box will appear that allows you to specify the name of the file to validate.
Both the inline and external versions of the XML sample file should validate successfully and display the same output.
Using Inline Schemas (XSD) | Example 1: Comparing an Inline Schema to an External Schema