XML Schema

Describing XML Documents

You can use a schema to describe the structure of an XML document and its type information. The two dominant technologies for defining an XML schema are document type definitions (DTDs) and XML Schema. You can use DTDs to define the structure of an XML document but not to describe the contents of a document. Here is an example:

<!-- Request Message --> <!ELEMENT PurchaseItem      (Quantity, Item)>     <!ELEMENT Quantity      (#PCDATA)>     <!ELEMENT Item          (#PCDATA)> <!-- Response Message --> <!Element PurchaseItemResult    (Amount)>     <!ELEMENT Amount            (#PCDATA)>

At first glance, it should be apparent that the DTD syntax is not XML-based. DTDs cannot be parsed using XML parsers and cannot be easily embedded into other XML documents. You will see why this is important for Web services when WSDL is discussed in the next chapter.

DTDs do describe the structure of the document, but they cannot express the type of data it contains. There is no notion of fundamental types within DTDs such as integers and strings, nor is there support for defining your own types.

In the preceding example, both the Quantity and the Item elements are declared as #PCDATA. This doesn't give you any insight into the type of data they can contain. For example, the DTD doesn't indicate whether it is valid to list partial quantities such as 1.5 cases. It also doesn't indicate whether the Item element should contain a numeric product ID or just a string containing a description of the item.

A proposed standard submitted to the W3C called Datatypes for DTDs (DT4DTD) 1.0 (http://www.w3.org/TR/dt4dtd) provides a means of grafting type information into DTD schemas. As of this writing, the proposal has been listed as a note for more than a year and a half and does not appear to be gaining much traction. DTDs should be considered legacy technology for defining XML schemas because of their limitations and lack of industry support.

The recommended way to express schemas for XML-based Web services is via XML Schema. XML Schema comprises two specifications managed by the W3C, XML Schema Part 1: Structures (http://www.w3.org/TR/xmlschema-1/) and XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/). As of May 2, 2001, both specifications are recommendations of the W3C.

XML Schema provides a rich syntax for defining schemas used to validate XML instance documents. It not only allows you to define the structure of an XML document, but it also allows you to define the type of data the document contains and any constraints on that data. Also, it lets you specify foreign key and referential integrity constraints. Here is a simple example:

<?xml version='1.0'?> <schema xmlns='http://www.w3.org/2001/XMLSchema'>   <!-- Response Message (work-in-progress) -->   <element name='Amount'/> </schema>

As you can see, the sample schema is a valid XML document that is capable of being consumed by any standard XML parser. A schema definition is contained within a root schema element. The example schema defines one element named Amount.

XML documents that can be validated against a schema are called instance documents. The following is an instance document for the schema described previously:

<?xml version='1.0'?> <Amount>351.43</Amount>



Building XML Web Services for the Microsoft  .NET Platform
Building XML Web Services for the Microsoft .NET Platform
ISBN: 0735614067
EAN: 2147483647
Year: 2002
Pages: 94
Authors: Scott Short

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net