Introduction to XML for Database Developers - XML–Data Reduced (XDR) Schema
(Page 4 of 9 )
When SQL Server 2000 was released, the W3C was still working on its XML Schema specification—it was not even clear which variation would be adopted. Microsoft has implemented a variation of XML schema syntax called XML–Data Reduced (XDR) in the MSXML parser (Microsoft XML Core Services) that was delivered initially as a part of Internet Explorer 5, and later in SQL Server 2000.
Microsoft promised complete support for XML Schema when the W3C awarded it Recommended status, but before that could happen, more and more organizations started using XDR. It is also important to note that Microsoft uses XDR in BizTalk, one of the most significant initiatives in the Web Services market. It is an initiative intended to create e-commerce vocabularies for different vertical markets.
At the time of this writing, SQL Server 2000 is using XDR schemas for several features, and support for XML Schema is increasing with each SQL Server 2000 Web Release.
The following is an example of an XDR schema document:
<Schema name="Schema"
xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="Inventory" content="empty" model="closed">
<AttributeType name="Inventoryid" dt:type="i4"/>
<AttributeType name="EquipmentId" dt:type="i4"/>
<AttributeType name="LocationId" dt:type="i4"/>
<AttributeType name="StatusId" dt:type="ui1"/>
<AttributeType name="LeaseId" dt:type="i4"/>
<AttributeType name="LeaseScheduleId" dt:type="i4"/>
<AttributeType name="OwnerId" dt:type="i4"/>
<AttributeType name="Rent" dt:type="fixed.14.4"/>
<AttributeType name="Lease" dt:type="fixed.14.4"/>
<AttributeType name="Cost" dt:type="fixed.14.4"/>
<AttributeType name="AcquisitionTypeID" dt:type="ui1"/>
<attribute type="Inventoryid"/>
<attribute type="EquipmentId"/>
<attribute type="LocationId"/>
<attribute type="StatusId"/>
<attribute type="LeaseId"/>
<attribute type="LeaseScheduleId"/>
<attribute type="OwnerId"/>
<attribute type="Rent"/>
<attribute type="Lease"/>
<attribute type="Cost"/>
<attribute type="AcquisitionTypeID"/>
</ElementType>
</Schema>
This XDR schema describes the structure of an XML document that contains Inventory information. The schema describes just one element—ElementType. The definition also specifies its name ("Inventory"), content (the tag is "empty" because all information will be carried in attributes), and content model ("closed"—indicating that it is not possible to add elements that are not specified in the schema).
The element contains several attributes. Each attribute is first defined in an AttributeType element and then instantiated in an attribute element:
<AttributeType name="Cost" dt:type="fixed.14.4"/>
...
<attribute type="Cost"/>
For each attribute, the schema defines a name and a data type. You can see a list of acceptable data types in the appendix at the end of this book.
The following listing shows an XML document that complies with the previous XDR schema:
<Inventory xmlns="x-schema:Schema.xml"
Inventoryid="5"
EquipmentId="1"
LocationId="2"
StatusId="1"
LeaseId="1"
LeaseScheduleId="1"
OwnerId="1"
Cost="1295.0000"
AcquisitionTypeID="1"/>
Schema Constraints This section reviews XDR schema attributes that can be used to declare elements and attributes. These can be classified as
- Element constraints
- Attribute constraints
- XML data types
- Group constraints
Element Constraints Elements in an XDR schema can be constrained using attributes of the <ElementType> tag:
- name
- content
- model
- order
- group
- minOccurs
- maxOccurs
The name attribute defines the name of the subelement.
Possible values for the content attribute are listed in the Table 13-1.
content | Meaning |
"textOnly" | Only text is allowed as content |
"eltOnly" | Only other elements are allowed as content |
"empty" | No content |
"mixed" | Both text and elements are allowed as content |
Table 13-1. content Attribute Values
An important innovation in XDR schemas (that was not available in DTDs) is the capability to add nondeclared elements and attributes to an XML document. By default, every element of every XML document has its model attribute set to "open". To prevent the addition of nondeclared elements and attributes, the model attribute has to be set to "closed".
It is also possible to define how many times a subelement can appear in its parent element by using the maxOccurs and minOccurs attributes. Positive integer values and "*" (unlimited number) are allowed in the maxOccurs attribute, and "0" and positive integer values are allowed in the minOccurs attribute. The default value for minOccurs is "0". The default value for maxOccurs is "1", except that when the content attribute is "mixed", maxOccurs must be "*".
An order attribute specifies the order and quantity of subelements (see Table 13-2). The default value for order is "seq" when the content attribute is set to "eltOnly" and is "many" when the content attribute is set to "mixed".
Attribute Constraints By their nature, attributes are more constrained than elements. For example, attributes do not have subelements (or subattributes), and it is not possible to have more than one instance of an attribute within the element.
The required attribute (constraint) in a schema specifies that the attribute is mandatory in XML documents that follow the schema. The default attribute
order | Meaning |
"seq" | Subelements must appear in the order listed in the schema. |
"one" | Only one of the subelements listed in the schema can appear in the XML document. |
"many" | Any number of subelements can appear in any order. |
Table 13-2. order Attribute Values of <ElementType>
(constraint) in a schema specifies the default value of the attribute in an XML document (the parser will use that value if an attribute is not present).
The schema can be set so that an attribute value is constrained to a set of predefined values:
<AttributeType name="status"
dt:type="enumeration"
dt:values="open in-process completed" />
XML Data Types The schema can also enforce the data type of the attribute or element. Table A-2 in the appendix lists data types and their meanings, and Table A-3 in the appendix shows the mapping between XML data types and SQL Server data types.
Next: Group Constraints >>
More MS SQL Server Articles
More By McGraw-Hill/Osborne
|
This article was excerpted from chapter 13 of SQL Server 2000 Stored Procedure & XML Programming, second edition, written by Dejan Sunderic (McGraw-Hill/Osborne, 2004; ISBN: 0072228962). Check it out at your favorite bookstore today. Buy this book now.
|
|