Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
November 4, 1998
The W3C XML Activity Page states: "While XML 1.0 supplies a mechanism, the Document Type Definition (DTD) for declaring constraints on the use of markup, automated processing of XML documents requires more rigorous and comprehensive facilities in this area. Requirements are for constraints on how the component parts of an application fit together, the document structure, attributes, datatyping, and so on. The W3C XML Schema Working Group is addressing means for defining the structure, content and semantics of XML documents."
The XML Schema support in Internet Explorer 5 Beta is a technology preview based on the XML-Data note submitted to the W3C. The XML Schema, as implemented in this technology preview, can be thought of as the subset of the XML-Data submission that corresponds to the feature set proposed for Document Content Description (DCD)
.
The XML parser in Microsoft's Internet Explorer 5 Beta can validate an XML document against both a Document Type Declaration (DTD) and an XML Schema. An XML Schema is an XML-based syntax for declaring content models. It has all the functionality of a DTD but with additional functionality like data typing.
Run the mouse over the following XML document to reveal the schema declarations for each node:
<class xmlns="x-schema:classSchema.xml"> <student studentID="13429"> <name>Jane Smith</name> <GPA>3.8</GPA> </student> </class>
You'll notice in the above document that the default namespace is "x-schema:classSchema.xml". This tells the parser to validate the entire document against the Schema (x-schema) at the following URL ("classSchema.xml").
Below is the entire Schema for the above document. Notice the namespace declarations on the root element of the Schema. The first (xmlns="urn:schemas-microsoft-com:xml-data") indicates that this XML document is an XML Schema. The second (xmlns:dt="urn:schemas-microsoft-com:datatypes") allows the Schema author to type element and attribute content by using the "dt" prefix on the "type" attribute within their "ElementType" and "AttributeType" declarations.
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes"> <AttributeType name='studentID' dt:type='string' required='yes'/> <ElementType name='name' content='textOnly'> <ElementType name='GPA' content='textOnly' dt:type='float'/> <ElementType name='student' content='mixed'> <attribute type='studentID'/> <element type='name'/> <element type='GPA'/> </ElementType> <ElementType name='class' content='eltOnly'> <element type='student'/> </ElementType> </Schema>
The Schema begins with the "Schema" element containing the declaration of the Schema namespace and, in this case, the declaration of the datatypes namespace as well. The content of the Schema begins with the "AttributeType" and "ElementType" declarations of the innermost elements:
<AttributeType name='studentID' dt:type='string' required='yes'/> <ElementType name='name' content='textOnly'> <ElementType name='GPA' content='textOnly' dt:type='float'/>
These declarations are then followed by the "ElementType" declaration of the parent to the elements just declared:
<ElementType name='student' content='mixed'> <attribute type='studentID'/> <element type='name'/> <element type='GPA'/> </ElementType>
This process is continued throughout the rest of the Schema, until every element and attribute has been declared.
Unlike DTDs, XML Schemas allow you to have an open content model, allowing you to do such things as type elements and apply default values without necessarily restricting content.
In the following Schema, the "GPA" element is typed and has an attribute with a default value, but no other nodes are declared within the "student" element:
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes"> <AttributeType name="scale" default="4.0"/> <ElementType name="GPA" content="textOnly" dt:type="float"> <attribute type="scale"/> </ElementType> <AttributeType name="studentID"/> <ElementType name="student" content="eltOnly" model="open" order="many"> <attribute type="studentID"/> <element type="GPA"/> </ElementType> </Schema>
The above Schema allows you to only validate the area with which you are concerned. This gives you more control over the level of validation for your document and allows you to use some of the features provided by the Schema without having to employ strict validation.
The Samples area of the Site Builder Network provides a set of XML sample files, including an XML document with an accompanying Schema. Download these samples and play around with the XML document and the Schema. To test the validity of your XML against a Schema, you can load the document through the the XML Validator or simply view the XML file in the Mime Type Viewer.
A few considerations:
Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.