Introduction to XML for Database Developers - Group Constraints
(Page 5 of 9 )
The group element allows an author to apply certain constraints to a group of subelements. In the following example, only one price (rent, lease, or cost) can be specified for the Inventory element:
<Schema name="Schema" xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes"> <ElementType name="Inventory" content="eltOnly"
model="closed" order="many">
<element type="Inventoryid"/>
<element type="EquipmentId"/>
<element type="LocationId"/>
<element type="StatusId"/>
<element type="LeaseId"/>
<element type="LeaseScheduleId"/>
<element type="OwnerId"/>
<group order = "one">
<element type="Rent"/>
<element type="Lease"/>
<element type="Cost"/>
</group>
<element type="AcquisitionTypeID"/>
</ElementType>
</Schema>
The group constraint accepts order, minOccurs, and maxOccurs attributes.
XML Schema (XSD) In May of 2001, XML Schema was given Recommended status by the W3C. Unfortunately, this stamp of approval happened after Microsoft had already released SQL Server 2000. However, in subsequent web releases of XML for SQL and SQLXML, and in releases of other products such as Visual Studio .NET, Microsoft has added support for XML Schema.
You can find the W3C XML Schema Recommendation specification, tools, and other resources at www.w3.org/XML/schema.html. I will try, however, to introduce the most important concepts.
The purpose of XML Schema is to define a class of XML documents. Each document of a specified class is an instance of that XML document class. The Equipment.xsd file contains an XML Schema document that defines instances of XML documents with Equipment information:
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace=
"http://www.trigonblue.com/Equipment.xsd"
xmlns=
"http://www.trigonblue.com/Equipment.xsd"
xmlns:mstns=
http://www.trigonblue.com/Equipment.xsd
xmlns:msdata=
"urn:schemas-microsoft-com:xml-msdata"
elementFormDefault="qualified"
attributeFormDefault="qualified">
<xsd:element name="Document">
<xsd:complexType>
<xsd:choice maxOccurs="Unbounded">
<xsd:element name="Equipment">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EquipmentId"
msdata:ReadOnly="true"
msdata:AutoIncrement="true"
type="xsd:int" />
<xsd:element name="Make" type="xsd:string" />
<xsd:element name="Model" type="xsd:string" />
<xsd:element name="EqTypeId"type="xsd:short" />
<xsd:element name="ModelSDX" type="xsd:string" />
<xsd:element name="MakeSDX" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
<xsd:unique name="DocumentKey1" msdata:PrimaryKey="true">
<xsd:selector xpath=".//mstns:Equipment" />
<xsd:field xpath="mstns:EquipmentId" />
</xsd:unique>
</xsd:element>
</xsd:schema>
You may notice that all elements in all XML Schema documents have an xsd prefix. Therefore, they are often stored in .xsd files and referred to as XSD schemas.
An XSD schema defines the structure and the types of data that can be used in a valid XML document instance. The following XML document is a valid instance of the previous schema:
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="http://www.trigonblue.com/Equipment.xsd"
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="Equipment.xsd">
<Equipment>
<EquipmentId>478</EquipmentId>
<Make>Compaq</Make>
<Model>15 Cart. DLT Library Tabletop Conversion Kit </Model>
<EqTypeId>1</EqTypeId>
</Equipment>
<Equipment>
<EquipmentId>394</EquipmentId>
<Make>Compaq</Make>
<Model>2KVA Prestige W/Ext full Bat</Model>
<EqTypeId>1</EqTypeId>
</Equipment>
<Equipment>
<EquipmentId>347</EquipmentId>
<Make>Compaq</Make>
<Model>Deskpro EN CMT PIII 733 10GB 128MB 48xCD nVidia NT</Model>
<EqTypeId>1</EqTypeId>
</Equipment>
...
</Document>
When an instance of an XML document and an XSD schema are processed together in a schema validator, the program checks whether the instance complies with the business rules defined in the schema and reports the result to the caller.
xsd:schema Element All XML Schema documents must contain the xsd:schema root element:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace= http://www.trigonblue.com/Equipment.xsd
xmlns=http://www.trigonblue.com/Equipment.xsd
xmlns:mstns=
"http://www.trigonblue.com/Equipment.xsd"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
elementFormDefault="qualified"
attributeFormDefault="qualified">
...
</xsd:schema>
The xmlns:xsd attribute notifies a parser (or any other agent) that all elements with an xsd prefix should be processed as XSD schemas. You must be very careful to include a reference to the namespace using the URL indicated in the preceding listing. Otherwise, parsers and validators will not recognize it and will not be able to process the XSD schema properly. All components of an XSD schema (such as elements, types, sequence, schema) are defined in this namespace.
The targetNamespace attribute specifies the namespace of a target XML document instance. The XML document instance must have a matching namespace declaration.
The xmlns attribute defines the default name of the namespace in the XML document instance. When you set elementFormDefault and attributeFormDefault to “qualified,” all elements and attributes defined in the XSD schema (not just global elements and attributes) will belong to the target namespace in the XML document instance, and they must be namespace qualified (that is, they must contain the appropriate prefix).
Structure Declarations and Definitions The primary tasks of the developer (or program) writing an XSD schema are to
- Declare the components of an XML document instance (elements and attributes)
- Define the components that are used inside the XSD schema (such as simple and complex types and attribute and model groups)
Element and Attribute Declaration xsd:element and xsd:attribute are used to declare elements and attributes in an XML document instance. In their simplest forms, elements and attributes can be defined by name and type (data type). In the following case, the LocationId attribute is defined as int and the Location element is defined as string:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Location" type="xsd:string"/>
<xsd:attribute name="LocationId" type="xsd:int"/> </xsd:schema>
Attributes can be declared based only on simple types, while elements can be declared based on both simple and complex types (I will define types in the next two sections).
Attributes and elements can be defined either globally (just below xsd:schema), as in the previous example, or inside other elements and complex types.
Next: Simple Type Declarations >>
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.
|
|