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.
<?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:
<catalog>
element, the XML schema instancing ("xsi:
") namespace is declared.<book>
element, the xsi:schemaLocation
schema instance attribute is used to declare that the contents of the <book>
element can be validated against a schema contained in an external XSD file (books1.xsd), located in the same directory.<?xml version="1.0"?> <catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> <bookxsi: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>
<?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>
'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!
<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.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 2: Adding a Namespace to the Schema