Referencing XSD Schemas in Documents

 

To reference an XML Schema (XSD) schema from an XML document in MSXML 6.0, you can use any one of the following means to link a schema to an XML document so that MSXML will use the schema to validate the document contents.

  • Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation.

  • Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.

The following sections demonstrate each of these methods for referencing schemas and discuss the advantages or disadvantages of each approach.

Using noNamespaceSchemaLocation

The xsi:noNamespaceSchemaLocation attribute works well in situations where namespaces are not used within the XML document contents you want to validate.

For example, the following shows an XML document that references an external XSD schema, MyData.xsd, that is located on a Web server.

<z:catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="www.microsoft.com/zzz">
   <book xsi:noNamespaceSchemaLocation="http://www.example.com/MyData.xsd"
         id="bk101">
      <title>Presenting XML</title>
      <author>Richard Light</author>
   </book>
</z:catalog>

If the MyData.xsd file exists and is reachable at the Web location stated in the xsi:noNamespaceSchemaLocation attribute, it will be used to validate the <book> node and any of its children nodes that are not namespace prefixed. If a Web server is not available, you can also locate the XSD file in the same directory as the XML file above or use a file-system URL.

If you use a file-based URL, be sure to escape the URL value correctly when you write in the xsi:noNamespaceSchemaLocation attribute. For instance, if you have a schema file on your system at the following path:

C:\Documents and Settings\All Users\Application Data\My Application\MyData.xsd  

You need to reformat the URL string so it is escaped properly so it can be used validly like this to locate the file:

xsi:noNamespaceSchemaLocation="file://C://Documents and Settings//All Users//Application Data//My Application//MyData.xsd"  

Another possibility is to place the schema in the same directory as the XML that references it. In this case, you only need to specify the file name such as "Mydata.xsd" for MSXML to locate and use the file.

Note

If the Mydata.xsd file is not valid XML, MSXML will ignore it and validate for well-formed XML only. To avoid this type of outcome, its a good practice to always validate your schema files first prior to attempting to use them in validation of other documents.

For more information, see Example 1: Validating with noNamespaceSchemaLocation in either C/C++, Example 1: Validating with noNamespaceSchemaLocation (Visual Basic) or Example 1: Validating with noNamespaceSchemaLocation (JScript).

Using schemaLocation

The xsi:schemaLocation attribute works well in situations where namespace prefixes are explicitly declared and used in the XML document you want to validate.

The following example shows an XML document that references an external XSD schema, MyData.xsd for us in validating nodes that are in the 'urn:MyData' namespace URI , which is mapped to the "MyData:" namespace prefix.

<catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  
      xsi:schemaLocation="urn:MyData http://www.example.com/MyData.xsd"  
      <MyData:book xmlns:MyData="urn:MyData">  
         <MyData:title>Presenting XML</MyData:title>  
         <MyData:author>Richard Light</MyData:author>  
      </MyData:book>  
   </catalog>  

In order for the MyData.xsd file to be paired with and used you to validate elements and attribute nodes that start with the "MyData:", the schema needs to use and contain the following schema attributes:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            xmlns:MyData="urn:MyData"  
            targetNamespace="urn:MyData"  
            elementFormDefault="qualified">  

These attributes declare the 'urn:MyData' namespace URI and the "MyData:" namespace prefix so that they correspond identically to how these declarations were made in the XML file. If they do not match, the schema at the specified location would never be invoked during validation.

In this example, we show that the schema is Web-hosted at the 'http://www.example.com' Web site. You could, optionally, locate the XSD file in the same directory as the XML file above or use a file-system URL.

If you use a file-based URL, be sure to escape the URL value correctly when you write in the xsi:schemaLocation attribute. For instance, if you have a schema file on your system at the following path:

C:\Documents and Settings\All Users\Application Data\My Application\MyData.xsd  

You need to reformat the URL string so it is escaped properly so it can be used validly like this to locate the file:

xsi:schemaLocation="urn:MyData file://C://Documents and Settings//All Users//Application Data//My Application//MyData.xsd"  

Another possibility is to place the schema in the same directory as the XML that references it. In this case, you only need to specify the file name like this:

xsi:schemaLocation="urn:MyData MyData.xsd"  

for MSXML to locate and use the file.

Note

If the MyData.xsd file is not valid XML, MSXML will ignore it and validate for well-formed XML only. To avoid this type of outcome, its a good practice to always validate your schema files first prior to attempting to use them in validation of other documents.

For more information, see Example 2: Validating with schemaLocation in either Example 2: Validating with schemaLocation (C-C++), Example 2: Validating with schemaLocation (Visual Basic) or Example 2: Validating with schemaLocation (JScript).

Using a schema cache

MSXML also provides a means to connect and use a schema cache to store, load and connect a schema to an XML document, such as in the following VBScript code excerpt:

'Create the schema cache and add the XSD schema to it.
set oSC = CreateObject("MSXML2.XMLSchemaCache.6.0")
oSC.Add "urn:MyData", "http://www.example.com/MyData.xsd"
'Create the DOM document assign the cache to its schemas property.
set oXD = CreateObject("MSXML2.DOMDocument.6.0")
oXD.schemas = oSC
'Set properties, load and validate it in the XML DOM.

In this example, we show that the schema is Web-hosted at the 'http://www.example.com' Web site. You could, optionally, locate the XSD file in the same directory as the XML file above or use a file-system URL.

If you use a file-based URL, be sure to escape the URL value correctly when you write in the xsi:schemaLocation attribute. For instance, if you have a schema file on your system at the following path:

C:\Documents and Settings\All Users\Application Data\My Application\MyData.xsd  

You need to reformat the URL string so it is escaped properly so it can be used validly like this to locate the file:

oSC.Add "urn:MyData", "C://Documents and Settings//All Users//Application Data//My Application//MyData.xsd"  

Another possibility is to place the schema in the same directory as the application that uses the schema cache. In this case, you only need to specify the file name like this:

oSC.Add "urn:MyData", "MyData.xsd"  

for MSXML to locate and use the file. If MSXML cannot locate the file based on the information you provide for the second parameter, it will throw an error. To specify that the schema does not apply to a namespace, you can use an empty string for the first parameter like this:

oSC.Add "", "MyData.xsd"  

Note

If the MyData.xsd file is not valid XML, MSXML will ignore it and validate for well-formed XML only. To avoid this type of outcome, its a good practice to always validate your schema files first prior to attempting to use them in validation of other documents.

For more information, see Example 3: Validating with XMLSchemaCache in either Example 3: Validating with XMLSchemaCache (C-C++), Example 3: Validating with XMLSchemaCache (Visual Basic) or Example 3: Validating with XMLSchemaCache (JScript).