Microsoft XML SDK 2.6 - XML Developer's Guide

Using In-line Schemas

Instead of using an external schema file, Msxml2 allows you to include schemas in-line.

For example, the following XML document contains an in-line schema against which the "Student" element is validated:

<Root>
<Schema name="mySchema" xmlns="urn:schemas-microsoft-com:xml-data"
             xmlns:dt="urn:schemas-microsoft-com:datatypes">
   <ElementType name="age" dt:type="int"/>
   <ElementType name="Student" model="closed" content="eltOnly">
     <element type="age"/>
   </ElementType>
</Schema>

<a:Student xmlns:a="x-schema:#mySchema">
   <age>10</age>
</a:Student>
</Root>

The ID of the schema is indicated through the "name" attribute on the "Schema" element. If there are two schemas with the same ID, the first schema to appear in the document will be the schema validated against.

An element is associated with an in-line schema just as it is with an external schema, through the "x-schema:" syntax when defining that element's namespace identifier. With an external schema, that which follows "x-schema:" indicates the location of the external schema file. With an in-line schema, that which follows "x-schema:" indicates the name of the schema within the XML instance document (this is indicated by the # sign followed by the name).

Note that the "#" syntax following "x-schema:" refers to schema within the local document only. This syntax cannot be used to reference schema within an external document (for example, "x-schema:URI#name" is not allowed).

The in-line schema associated with an element must precede that element within the document. When validating against the schema, if the name indicated through an element's namespace declaration does not correspond to an already parsed schema, an error will occur. This behavior is consistent with the case in which an element is associated with a schema that does not exist.

The DOMDocument's validateOnParse property behaves the same with in-line schemas as it does on external schemas: if set to false, validation doesn't occur, but the default attributes, IDs and IDREFs, and datatypes are all processed according to the schema. Multiple schemas can appear in the instance document as long as they conform to the ordering rules outlined above and their IDs do not conflict.

You can read from/write to in-line schemas through the DOM. The XMLDOMNode's definition property returns the type declaration that corresponds to the node just as it does with an external schema. If the type declaration is changed through the DOM, then the definition property reflects these changes. If the type declaration is removed or the value of the name attribute is changed, the definition property returns null. If a new type declaration is added to the schema and a corresponding node is added to the instance data, the definition property for the new node returns the new corresponding type declaration.

In-line Schemas Persisted from ADO

You can use Msxml2 to parse in-line schemas produced by ADO, but they won't be validated. So given the following ADO data:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
   xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
   xmlns:rs='urn:schemas-microsoft-com:rowset'
   xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
   <s:ElementType name='row'>
      <s:attribute type='CustomerID'/>
      <s:extends type='rs:rowbase'/>
   </s:ElementType>
<s:AttributeType name='CustomerID' rs:number='1'
   rs:nullable='true' rs:write='true'>
   <s:datatype dt:type='string' dt:maxLength='5'/>
</s:AttributeType>
</s:Schema>
<rs:data>
   <z:row CustomerID='…'/>
</rs:data>
</xml>

Msxml2 essentially ignores the following elements and attributes, and the remainder of the schema is parsed as if it were a valid schema:

Elements Attributes
extends dt:max
  dt:maxExclusive
  dt:maxLength
  dt:min
  dt:minExclusive

Validation against the remainder of the schema cannot occur because the schema does not contain a "name" attribute and because the reference to the schema is not prefaced by "x-schema:". Msxml2 views that reference as a URN with no special significance.