Condividi tramite


Using Inline Schemas (XSD)

 

Beginning with MSXML 5.0 for Office Applications, support has been implemented to allow you to use inline XML schemas that conform to the XML Schemas (XSD) specification.

Inline schemas can be useful when it is inconvenient to physically separate the schema and the instance XML document. For example, if you generate an XSD schema from data retrieved from a database, it might be more convenient to put the schema and the data in the same file.

Note

Support for inline XDR schemas was previously implemented in MSXML 3.0 and 4.0. The use of XDR schemas is deprecated in MSXML 6.0, including the support of inline XDR schemas.

In MSXML 5.0 for Microsoft Office Applications, inline XSD schema support is enabled by default whenever you create a new DOM document or SAX stream. In MSXML 6.0, inline XSD schema support is disabled by default.

Inline schema support includes a compiling step for any inline schema contents, prior to subsequent parse/validate operations. This is done to ensure that the inline schema fragment is valid as an XML schema, before it is applied or used to validate the non-schema XML remainder of the DOM document or SAX stream.

The ability to control whether inline schemas are compiled and used to validate their host documents can be set in one of two ways, depending on whether you are working with the DOM or the SAX APIs:

  • For DOM programming, you can turn off the second-level UseInlineSchema property on the current DOMDocument object.

  • For SAX programming, use the putFeature method to turn off the "use-inline-schema" feature on the SAXXMLReader object.

If inline schema processing is turned off, inline schemas are treated no differently from any other XML document fragment, and any special meaning they have for validation is ignored.

Important

Because inline schema compilation can take place even when validation is turned off for the DOM or SAX instance, some parsing errors can occur if you compile inline schemas when you do not intend to validate the document. To avoid or resolve such errors, turn off inline schema support using the appropriate DOM or SAX mechanism, as mentioned above. If validation is turned off and inline schemas are disabled, schema compilation will not take place.

Inline schemas are referenced and handled in the same way as external schemas that are declared using either the schemaLocation attribute or the noNamespaceSchemaLocation attribute to specify a URI or path location to the external schema file.

Just like with external schemas, documents and document fragments validated using inline schemas must be identified by their namespace, which should match the targetNamespace attribute in the schema. For example, a schema with a target namespace of "http://example.com" is applied only to those fragments that also belong to the "http://example.com" namespace.

If a schema is used without targetNamespace, it is applied to the fragments, which belong to the default namespace. By design, inline schemas can never be applied to the root element.

In the following section, we will look at examples of how inline XSD schemas differ from external XSD schemas that reference an external location, such as another file or a schema cache.

Example: Adding a Namespace to an Inline Schema

In example 4: Validating using an Inline XSD Schema, we looked at a simple namespace-less example of how to use an inline XSD schema for validating all element and attribute nodes below the document root.

But what if you need to incorporate a schema that will validates nodes that are tied to a namespace? In this example, we will look at the changes needed if your inline XSD schema needs to be aware of namespaces. For the purposes of this example, we have determined that the <book> element needs to exist in a separate "x" namespace instead.

File: inline-namespaced.xml

<?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

<?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="urn: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"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <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>
Try It!
  1. Download iexmltls.exe from MSDN. The following page contains the download and installation instructions: https://www.microsoft.com/downloads/details.aspx?FamilyId=D23C1D2C-1571-4D61-BDA8-ADF9F6849DF9

  2. Alternatively, you can search for "Internet Explorer Tools for Validating XML and Viewing XSLT Output" to find the download page.

  3. Follow the instructions on the download page for downloading and installing the Internet Explorer XML tools.

  4. Open Notepad.

  5. 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.

  6. 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.

  7. 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.

  8. In Windows Explorer, double-click on inline-namespaced.xml to open it in Internet Explorer.

  9. In the browser window, right-click and select Validate XML.

  10. In Windows Explorer, double-click on external-namespaced.xml to open it in Internet Explorer.

  11. In the browser window, right-click and select Validate XML.

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

Output

A message similar to the following will appear in the browser window: Validation Successful. file:///Z:/Samples/Inline/inline-namespaced.xml