Introduction to XML for Database Developers - XSD Schema Tools
(Page 7 of 9 )
The XML development community, including Microsoft, has developed many useful tools for development and management of XSD schemas. You can find a comprehensive list with links at www.w3.org/XML/Schema#Tools. I will now demonstrate use of three tools developed by Microsoft.
XSD Designer Visual Studio .NET supports the use of XSD schemas primarily to process ADO.NET data sets. It contains the XSD Designer—a graphical tool that allows you to drag and drop relation tables to link them and then build XSD schema out of them. To use it:
- Open a new Visual Studio .NET project.
- Select File | Add Item from the menu.
- Select XSD Schema. The program will open the XSD Schema Designer with the Schema pane active.
- Open Server Explorer.
- Expand the server node until you reach the tables in the Asset database.
- Drag the Equipment table onto the XSD Schema Designer (see Figure 13-2).
- You can switch to the XML pane to see the code of the schema.

Figure 13-2. The XSD Schema Designer
The XSD Schema Designer generates schemas that have some additional elements and attributes that are needed to validate ADO.NET datasets:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs=http://www.w3.org/2001/XMLSchema
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Document">
<xs:complexType>
<xs:choice maxOccurs="Unbounded">
<xs:element name="Equipment">
<xs:complexType>
<xs:sequence>
<xs:element name="EquipmentId"
msdata:ReadOnly="true"
msdata:AutoIncrement="true"
type="xs:int" />
<xs:element name="Make" type="xs:string"
minOccurs="0" />
<xs:element name="Model" type="xs:string"
minOccurs="0" />
<xs:element name="EqTypeId" type="xs:short"
minOccurs="0" />
<xs:element name="ModelSDX" type="xs:string"
minOccurs="0" />
<xs:element name="MakeSDX" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="DocumentKey1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:Equipment" />
<:field xpath="mstns:EquipmentId" />
</xs:unique>
</xs:element>
</xs:schema>
The XSD is generated with temporary URI (tempuri.org) namespace references. You can replace them with your own namespaces:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace=http://www.trigonblue.com/Equipment.xsd
elementFormDefault="qualified"
xmlns="http://www.trigonblue.com/XMLSchema.xsd"
xmns:mstns= http://www.trigonblue.com/Equipment.xsd
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
XSD by Example: Microsoft XSD Inference Microsoft XSD Inference is a web-based utility that you can use to create an XSD schema from an XML instance document (see Figure 13-3). You can think of it as “XSD by example.” When you select a well-formed XML file, the utility generates an XSD schema that can be used to validate it. You can continue refining the XSD schema by selecting more XML files. At the end, you might also need to edit it manually to implement additional components such as facets (restrictions) and annotations.
XSD Schema Validator Another tool that you might find useful when working with XSD schemas is the XSD Schema Validator, a web application (see Figure 13-4) that

Figure 13-3. XSD by example

Figure 13-4. The XSD Schema Validator
you can find at http://apps.gotdotnet.com/xmltools/xsdvalidator/. If you load your schema and your XML document instance, the XSD Schema Validator will report whether or not the instance complies with rules specified in the schema.
NOTE
You cannot validate an instance if you simply put the instance and schema in a folder on an IIS server and open it using Internet Explorer. Unfortunately, the parser in IE is not set as a validating parser and will simply display the content of the document instance.
Linking and Querying in XML
XML today represents more than a simple language for encoding documents. W3C is working on a whole other set of specifications for using information in XML documents. Specifications such as XLink, XPointer, XPath, and XQL allow querying, linking, and access to specific parts of an XML document.
This is a vast topic, and I will briefly review only XPointer and XPath, since they are used in SQL Server 2000.
XPointer The XPointer reference works in a fashion very similar to the HTML hyperlink. You can point to a segment of an XML document by appending an XML fragment identifier to the URI of the XML document. A fragment identifier is often enclosed in xpointer(). For example, the following pointer directs the parser to an element with the ID attribute set to "Toshiba" in the document at a specified location:
http://www.trigonblue.com/xml/Equipment.xml#xpointer(Toshiba)
The character # is a fragment specifier. It serves as a delimiter between the URI and the fragment identifier, and it specifies the way that the XML parser will render the target. In the preceding case, the parser renders the whole document to access only a specified fragment. To force the parser to parse only the specified fragment, you should use | as a fragment specifier:
http://www.trigonblue.com/xml/Equipment.xml|xpointer(Toshiba)
Use of the | fragment specifier is recommended because it leads to reduced memory usage.
xpointer() is not always required. If a document has a schema that specifies the ID attribute of an element, you can omit the xpointer() and point to a fragment of the document using only the ID attribute value:
http://www.trigonblue.com/xml/Equipment.xml#Toshiba
Child sequence fragment identifiers use numbers to specify a fragment:
http://www.trigonblue.com/xml/Equipment.xml#/2/1/3
The preceding example should be interpreted as follows: /—start from the top element of the document; 2—then go to the second child element of the top element; 1—then go to the first subelement of that element; 3—then go to the third subelement of that element.
Child sequence fragment identifiers do not have to start from the top element:
http://www.trigonblue.com/xml/Equipment.xml#Toshiba/1/3
In this example, fragment identification starts from the element with its ID set to "Toshiba". The parser then finds its first subelement and points to its third subelement.
Next: XPath >>
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.
|
|