Tải bản đầy đủ (.pdf) (62 trang)

Tài liệu Module 8: Validating XML Data Using Schemas doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (981.11 KB, 62 trang )







Contents
Overview 1
The Need for Validation 2
Writing an XML Schema 15
Extending an XML Schema 34
Validating XML in a Client/Server
Environment 38
Lab 8: Validating XML Data Using Schemas 46
Review 54

Module 8: Validating
XML Data Using
Schemas


Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any


written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.

 2000 Microsoft Corporation. All rights reserved.

Microsoft, MSDN, MS-DOS, PowerPoint, and Windows are either registered trademarks or
trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

The names of companies, products, people, characters, and/or data mentioned herein are fictitious
and are in no way intended to represent any real individual, company, product, or event, unless
otherwise noted.

Other product and company names mentioned herein may be the trademarks of their respective
owners.


Program Manager: Steve Merrill
Instructional Designers: Sangeeta Nair (NIIT), Vijayalakshmi Narayanaswamy (NIIT)
Subject Matter Experts: Andy Olsen (QA Training), Andy Longshaw (Content Masters)
Content Lead: Janet Robinson
Graphic Artist: Scott Serna (Creative Assets)
Media Management: David Mahlmann
Media Production: Dean Connolly (Art Source), Timothy Demmon (:timebomb Media)
Editing Manager: Jennifer Linn
Editor: Dennis Rae (Wasser)
Production Manager: Miracle Davis
Print Coordinator: Marlene Lambert (Online Training Solutions, Inc)
Build Manager: Julie Challenger
Build Coordinator: Jenny Boe
Test Lead: Eric Myers

Manufacturing Manager: John Williams
Group Product Manager: Steve Elston

Module 8: Validating XML Data Using Schemas iii


Instructor Notes
This module describes how to create and use Microsoft XML schemas to
validate XML documents. Students have already been introduced to the concept
of validation in Module 2, “Overview of XML Technologies.” In that module,
we introduced the need for validation, showed the syntax of simple Document
Type Definitions (DTDs), and briefly mentioned the use of XML schemas as
the preferred alternative to DTDs.
In this module, students are now shown the full syntax for XML schemas, as
supported by Microsoft. The Microsoft standard for XML schemas is also
referred to as XDR (XML Data Reduced), and is different in several respects to
the schema definition in the W3C draft standard.
Once the XML schema syntax has been introduced, this module describes how
an XML schema can be applied to an XML document received at the server.
You should impress upon students the need to perform validation of XML data
at the server whenever it is received from an unknown client.
After completing this module, students will be able to:
!
Describe when validation is needed.
!
Create an XML schema.
!
Validate an XML document by using an XML schema.
!
Apply an XML schema to an XML document, both statically and

dynamically.

Materials and Preparation
This section provides you with the required materials and preparation tasks that
you need to teach this module.
Required Materials
To teach this module, you need the following materials:
!
Microsoft PowerPoint
®
file 1905a_08.ppt
!
Module 8, “Validating XML Data Using Schemas”
!
Lab 8, “Validating XML Data Using Schemas”

Preparation Tasks
To prepare for this module, you should:
!
Read all of the materials for this module.
!
Complete the labs.
Due to the length of the answers to the labs for this course, we were unable
to include them in the Delivery Guide. Please see Appendix A and the
Student CD for the lab answers.
!
Read the working draft specification for XML schemas on the W3C Web
site at
.
!

Read the latest information about Microsoft’s support for XML schemas at
/>.
Presentation:
150 Minutes

Lab:
60 Minutes
iv Module 8: Validating XML Data Using Schemas


Module Strategy
Use the following strategies to present this module:
!
The Need for Validation
This section sets the scene for the rest of the module — students need to
appreciate the need for validation before looking at the details. This section
begins by differentiating between the structure of an XML document (that
is, what elements and attributes are allowed where) and the semantics of an
XML document (for example, what value an <address> element can have).
You should emphasize that validation can help to identify what data is
allowed, but it does not specify how you should process that data.
You should also spend some time describing why a document might have an
invalid structure, whether due to programming error, malicious or accidental
corruption, or different versions of a document being used at the client and
server.
!
Writing an XML Schema
This is the main section of the module. It covers all the syntax for defining
XML schemas, and how to apply an XML schema to a static XML
document.

You might want to compare <ElementType> and <AttributeType> with type
definitions in other programming languages (for example, typedef in
Microsoft Visual C/C++). Also make sure that students understand the
difference between <ElementType> and <element type=...>; the former
introduces a new type, the latter indicates a usage of that type. The same
issue arises with <AttributeType> and <attribute type=...>.
In addition to pointing out the syntax, you should highlight the following
key differences between XML schemas and DTDs:
• XML schemas allow an open content model.
• XML schemas allow data types to be specified.
• XML schemas are extensible.
• XML schemas are applied to XML elements, rather than being applied
to an entire document (as is the case with DTDs).
• XML schemas are themselves XML documents. This allows for some
sophisticated programming techniques, such as XML schemas that are
definable and modifiable at run time.
!
Extending an XML Schema
Another key difference between XML schemas and DTDs is that XML
schemas allow you to add elements and attributes to the XML document
using the schema.
This would be useful if every XML document needed this new information.
Rather than adding the additional elements and attributes to every
document, you can add them in one place (the schema), and all XML
documents using that schema will have the new elements and attributes.
Module 8: Validating XML Data Using Schemas v


!
Validating XML in a Client/Server Environment

In this section, we show how to apply an XML schema to an XML
document in a more realistic client/server environment. Initially, we show
how the client can attach an XML schema before sending the document to
the server. In this scenario, when the document is loaded into an XMLDOM
object at the server, the document is automatically validated (as long as the
validateOnParse flag is set to True, which is the default).
However, when the server loads the XML document, it has no way of
knowing in advance whether an XML schema is present. Only when the
document has been loaded can the server check for the XML schema. If
there is no XML schema attached, the server can programmatically apply
the XML schema, then load the XML document (plus the XML schema)
into a new XMLDOM object to enable the document to be validated.
At the server, you cannot reference an external XML schema by specifying
a URL in the xmlns attribute — you can only use schemas found on the local
Web server. For more information, search for the Knowledge Base article
#Q235344: “FIX: DTDs and Schemas Not Resolved When Using loadXML
Method” on the MSDN™ Web site at
.
!
Lab 8: Validating XML Data Using Schemas
This is the final lab in the course, and completes the LitWare Books Web
application. By this point in the course, students should have a good grasp
of the application and be comfortable with the flow of XML data between
the client and the server.
In this lab, students write a new XML schema to validate the XML customer
order submitted from the client during checkout.
The first exercise takes the majority of the allocated time for this lab. In this
exercise, students write the XML schema and test it against a static XML
document to ensure that the XML schema is correct.
In the second exercise, students use the XML schema in the context of the

LitWare Books Web application. Specifically, when CustomerOrder.asp
receives an XML data packet representing a customer order, the ASP
applies the XML schema and tests the document for validity. A suitable
error message, in XML format, is returned if the XML data is invalid.


Module 8: Validating XML Data Using Schemas 1


#
##
#

Overview
!
The Need for Validation
!
Writing an XML Schema
!
Extending an XML Schema
!
Validating XML in a Client/Server Environment
!
Lab 8: Validating XML Data Using Schemas
!
Review


One of the key aims of XML is to facilitate the open exchange of information
between applications and organizations. Clearly, this is only possible if the

structure or the format of these XML documents can be agreed upon and
defined.
The proper format of an XML document may be defined in either an XML
schema or a Document Type Definition (DTD). Once defined, the XML schema
or DTD can be used to validate the contents of an XML document.
XML schemas offer several technical advantages over DTDs. This module
describes how to create an XML schema, and shows you how to apply the
schema to an XML document. You will also learn how to apply an XML
schema to an XML document dynamically when the document is received at
the Web server.
After completing this module, you will be able to:
!
Describe when validation is needed.
!
Create an XML schema.
!
Validate an XML document by using an XML schema.
!
Apply an XML schema to an XML document, both statically and
dynamically.

Slide Objective
To provide an overview of
the module topics and
objectives.
Lead-in
In this module, you will learn
how to use XML schemas to
validate XML documents in
a Web-based application.

Validation is essential in
order to guarantee the
correct structure of XML
data.
2 Module 8: Validating XML Data Using Schemas


#
##
#

The Need for Validation
!
What Can Be Validated: Structure
!
What Cannot Be Validated: Semantics
!
Detecting Incorrect Documents Using Validation
!
Demonstration: Validating XML Data
!
XML Schemas vs. DTDs
!
Causes of Invalid XML Documents
!
Handling Invalid Documents


In this section, you will learn about the need for validation when XML
documents are exchanged within an application, between applications in the

same organization, and across organizational boundaries.
This section highlights what can and cannot be achieved by using XML
schemas or DTDs to validate XML documents, and also describes some of the
typical causes of invalid XML documents.
Slide Objective
To provide an overview of
the topics in this section.
Lead-in
This section introduces the
need for validation of XML
data, and describes the
benefits of using XML
schemas rather than DTDs
to achieve validation.
Module 8: Validating XML Data Using Schemas 3


What Can Be Validated: Structure
!
When XML documents are exchanged, it is necessary to
agree on the structure of the documents
!
XML schemas and DTDs define the structure of XML
documents
Elements
What elements are allowed?
What child elements are required or optional?
Is the order or number of child elements important?
What content type is allowed?
Attributes

What attributes are required or optional?
What are the data types of the attributes?
Are there any meaningful default values?


XML schemas and DTDs define the element and attribute types that are
allowed in an XML document. XML schemas allow a more rigorous definition
of document structure than DTDs.
The structure rules should include the following details about elements in the
XML document:
!
What element types are recognized?
!
Which elements are child elements of other elements?
!
How many child elements are allowed?
!
Is the sequence of child elements significant? If so, what is the correct
sequence?
!
What type of content is allowed for each element?

The structure rules should also include the following details about attributes:
!
What attribute types are recognized?
!
What attributes are required for each element type?
!
Are the attributes required or optional?
!

What is the data type of each attribute?
!
Is there a restricted set of values that can be used for each attribute?
!
Is there a default value for each attribute?

Slide Objective
To describe how the
structure of an XML
document can be tested by
validation.
Lead-in
XML schemas and DTDs
were introduced earlier in
the course as a means of
defining the format of XML
documents.
4 Module 8: Validating XML Data Using Schemas


What Cannot Be Validated: Semantics
!
Data cannot be meaningfully processed without
validating its semantics
!
DTDs and XML schemas provide validation tests for
structure, not semantics
!
Solution:
$

Define different XML element types for each element
$
Add an attribute to describe the element type
<postal-address>12 Main Street</postal-address>
<email-address></email-address>
<postal-address>12 Main Street</postal-address>
<email-address></email-address>
<address type="postal">12 Main Street</address>
<address type="email"></address>
<address type="postal">12 Main Street</address>
<address type="email"></address>


If two companies wish to exchange information by using XML, an accurate
XML schema or DTD is essential. However, the sender and receiver of the
document still need to understand not only the structure but also the semantic
meaning of the XML data. Data cannot be meaningfully processed unless its
semantics are verifiable
Without this understanding of the semantics, it is quite possible to create an
XML document that complies with the expected format but is still misleading.
Example of ambiguous semantics
Consider a situation where an XML schema or DTD specifies an <address>
element for an XML document. There is scope for confusion and
misinterpretation here — “address” might mean a postal address, an e-mail
address, or even a URL for a Web site.
XML schemas and DTDs can verify that the XML document contains an
<address> element, but cannot determine the correct type of the address
supplied.
The easiest solution is to define different XML element types for each type of
address, as in the following example:

<postal-address>12 Main Street</postal-address>
<email-address></email-address>

Slide Objective
To explain why the semantic
meaning of an XML
document cannot be tested
by validation.
Lead-in
Just because an XML
document has the correct
structure, it doesn’t
necessarily follow that the
data is meaningful.
Module 8: Validating XML Data Using Schemas 5


A slight variation on this theme might be to use an <address> element for each
type of address as before, but to add an attribute to describe what type of
address is being represented:
<address type="postal">12 Main Street</address>
<address type="email"></address>

This approach preserves the semantic differences between the different address
types, but prevents the proliferation of new element types that might clutter up
an XML document.
6 Module 8: Validating XML Data Using Schemas


Detecting Incorrect Documents Using Validation

!
Defining an XML schema or DTD
$
Specify the structure for a class of XML documents
!
Applying XML schemas or DTDs
$
When you create an XML document, or
$
When you receive an XML document from elsewhere
!
Enabling the XML parser to validate the document
$
Test the outcome of the validation operation


Validating XML documents involves the following steps.
Defining an XML schema or DTD
The first step in validating an XML document is to define an XML schema or
DTD for that type of document. The XML schema or DTD defines the structure
for all documents of the same type.
For example, the LitWare Books Web application generates many XML
documents with <booklist> information. Each XML document contains a
different list of books, but the structure of each document is the same.
Therefore, it is only necessary to define a single XML schema or DTD, which
applies to all of these documents.
Applying XML schemas and DTDs
An XML schema or DTD can be applied to an XML document when it is
created by the document author. Any recipient of the XML document will have
immediate access to the XML schema or DTD for that document, and therefore

will be able to validate the document.
Alternatively, the document author can choose to omit the XML schema or
DTD initially. Recipients can then apply the XML schema or DTD themselves
when the document is received. This entails more work for the recipient, but
allows the recipient to be sure that the correct XML schema or DTD is applied.
This is the preferred method in a client/server environment because a server that
receives XML data from clients cannot be sure that an XML schema or DTD
has been applied beforehand.
Slide Objective
To describe the process for
validating XML documents.
Lead-in
There are certain tasks you
must undertake if you want
your XML documents to be
validated when they are
loaded.
Module 8: Validating XML Data Using Schemas 7


Enabling the parser
When an XML schema or DTD is applied to an XML document, validating
parsers will check the structure of the document as it is loaded. Any validation
errors will cause the document loader to abort.
You can perform validation at either the client or the server. For example, the
client might wish to confirm that an XML document received from the server is
valid. Conversely, the server might need to validate XML posted from the
client. Depending on where you wish to perform validation, you can write
client-side script or server-side script to test the outcome of the validation
operation, and respond accordingly.

8 Module 8: Validating XML Data Using Schemas


Demonstration: Validating XML Data


In this demonstration, you will see how to install a simple validation utility that
can be integrated with Internet Explorer 5.0 to provide immediate feedback on
the validity of an XML document loaded in Internet Explorer 5.0.
The validation utility is available on the Student CD-ROM in the folder
\Sampapps\xml diagnostics\xml validation.
!
To install the XML Validation utility
• On the Student CD-ROM, right-click the file msxmlval.inf in the folder
\Sampapps\xml diagnostics\xml validation, and then click Install.

Slide Objective
To show how to install a
validation utility that can be
integrated with Internet
Explorer 5.0 in order to test
the validity of XML
documents.
Lead-in
Microsoft provides a simple
tool that can be integrated
with Internet Explorer 5.0,
so that you can get
immediate feedback about
the validity of an XML

document in Internet
Explorer 5.0.
Delivery Tip
1. Open Windows Explorer
and navigate to the folder
\InetPub\WWWRoot\1905\D
emoCode\Mod08. Note that
the folder contains files
named msxmlval.htm and
msxmlval.inf.
2. Right-click msxmlval.inf,
and then click Install to
install the validator utility.
3. Start Internet Explorer 5.0
(or restart it if it is already
running) and open
books.xml from the same
folder.
4. Right-click anywhere in
the main window of Internet
Explorer 5.0, and then click
Validate XML. A message
box appears confirming that
this is a valid document.
5. In Notepad, open
books.xml and edit it to
make it invalid, for example,
by deleting a <price>
element.
6. Refresh the view in

Internet Explorer 5.0 and
test the validity of books.xml
again. An error message
appears.
Module 8: Validating XML Data Using Schemas 9


XML Schemas vs. DTDs
!
XML schemas
$
Allow an “open” content model
$
Support data type definitions
$
Are extensible
$
Can be applied to individual elements
$
Are accessible in DOM
!
DTD-to-Schema conversion utility


XML schemas and DTDs have a similar purpose, but XML schemas offer more
precision and flexibility. The following list summarizes the key differences
between XML schemas and DTDs:
!
Open content model
XML schemas can define an “open” content model. This enables additional

elements and/or attributes to be present within an element without having to
declare each and every element in the XML schema. In contrast, DTDs
define a “closed” model, which means a document cannot contain additional
content except that explicitly defined in the DTD.
!
Data type for every element or attribute
XML schemas allow you to specify a data type for an element or attribute.
Data types indicate the format of the data, provide for validation of the type
by the XML parser, and enable processing specific to the data type in the
DOM. DTDs do not support data types.
!
Extensible
XML schemas are extensible; that is, custom schemas can be built from
standard schemas. For example, let’s imagine that there is a definition for an
<address> element in another schema, and that this definition meets your
current needs for an <address> element. With namespaces, this schema can
be used directly with a reference to the original schema, rather than copying
the definition.
Only one DTD document can be attached per XML document.
Slide Objective
To describe the technical
advantages of XML
schemas as compared to
DTDs.
Lead-in
DTDs are established and
standardized, but XML
schemas are more powerful.
Microsoft recommends the
use of XML schemas for the

future.
10 Module 8: Validating XML Data Using Schemas


!
Apply to individual elements
XML schemas are applied to specific elements. Generally, you might want
to apply an XML schema to the root element of the XML document in order
to define the grammar for the entire document. However, you can also apply
an XML schema to isolated elements in order to define the grammar for
portions of the XML document. This is different from the situation with
DTDs, where only a single DTD can be applied to the entire document.
!
Regular XML syntax
XML schemas use regular XML syntax rather than the non-XML syntax
used in DTDs. One of the interesting consequences of this is that you can
load an XML schema into the DOM and programmatically access the
grammar rules within your script.

For more information on XML schemas vs. DTDs, refer to Module 2,
“Overview of XML Technologies.”
DTD-to-Schema conversion utility
A DTD-to-Schema conversion utility is included on the Student CD-ROM in
the folder \Sampapps\DTD to Schema Conversion. This utility will translate a
DTD into a schema.
!
To run the DTD-to-Schema conversion utility
1. Open Windows
®
Explorer and navigate to the folder \Sampapps\DTD to

Schema Conversion on the Student CD-ROM.
This folder contains the dtd2schema.exe utility.
2. Open a Command window and navigate to this folder.
3. Run dtd2schema.exe as follows:
dtd2schema -o myxmlfile.xml mydtdfile.dtd

This creates an XML schema named myxmlfile.xml.

Delivery Tip
This is an important
distinction. DTD’s apply to
the entire document. With
schemas, you can have
different schemas for
different elements.
Delivery Tip
As an optional
demonstration, you can
describe how to use the
DTD-to-Schema conversion
utility to translate a DTD into
a schema.
1. Open Windows Explorer
and navigate to the folder
\InetPub\WWWRoot\1905\D
emoCode\Mod08, which
contains the
dtd2schema.exe utility.
2. Open a Command
window and navigate to this

folder.
3. Run dtd2schema.exe as
follows:
dtd2schema
-o newbookschema.xml
books.dtd.
This creates an XML
schema named
newbookschema.xml.
4. Open Notepad and view
newbookschema.xml. We
haven’t discussed schema
syntax yet, so don’t go into
too much detail. Just pick
out some of the
<ElementType>
statements.
Module 8: Validating XML Data Using Schemas 11


Causes of Invalid XML Documents
!
XML mismatches can occur between the client
and server
!
Reasons
$
Version skew
$
Malicious alteration of XML data

$
Misinterpretation of XML schema or DTD
$
Programming error


There are several situations that might give rise to an invalid document being
delivered to a Web server. Some of the possibilities are outlined below.
Version skew
Versioning is an issue that affects many aspects of the Information Technology
industry, and XML is no exception. A client program might build an XML
document according to a previous version of an XML schema/DTD, unaware
that a newer version exists on the server.
The server must ensure that the correct XML schema/DTD is applied to the
document, and validate the document against this grammar.
Malicious alteration of XML data
When an XML document is submitted from a client to a server, the possibility
exists that the document might be tampered with in transit — document content
might be maliciously added, modified, or deleted.
The server must be capable of validating the XML data as it is received, rather
than assuming that if the XML data was valid at the client, it is still valid when
received at the server.
Misinterpretation of XML schema or DTD
XML schemas and DTDs perform a dual role. As well as enabling validation to
take place when an XML document is loaded, they also act as a source of
documentation for application developers, telling them what structure of XML
to build.
XML schemas and DTDs can be quite complex to read and understand, and the
developer might misinterpret the grammar rules and build an invalid XML
document by mistake.

Slide Objective
To describe some of the
situations that give rise to
invalid XML documents.
Lead-in
There are several reasons
why invalid XML documents
get created in the first place.
12 Module 8: Validating XML Data Using Schemas


Programming error
Programming errors do occur, and applications need to be robust enough to
detect these errors and handle them in a reliable and meaningful manner.
In a Web-based XML application, a developer might forget to add some
important elements or attributes to an XML document before it is sent to the
Web server. Another possible mistake might be to use invalid data types for
elements or attributes, or to supply values that are out of range.
Consider Checkout.htm in the LitWare Books Web application. Checkout.htm
builds an XML document by using the shopping basket XML data island, and
adds the customer ID entered by the user. LitWare Books expects integer values
for the customer ID, but the user can enter any type of value in the text field.
The developer should ensure that an integer is entered, rather than accepting
anything and sending it untested to the Web server.
Module 8: Validating XML Data Using Schemas 13


Handling Invalid Documents
!
If an XML document is found to be invalid, several

options are available:
$
Reject the document and issue an error message
$
Use the DOM to access the valid parts of the document
$
Use XSL to transform the document into a valid format
$
Ignore the validation error


When an invalid document is received, you can choose to abandon the
document altogether and issue an error message to its creator. Alternatively,
you can try to recover as much meaningful data from the document as possible,
and continue on that basis.
Another course of action might be to ignore the validation error altogether and
carry on regardless. This might be appropriate if you simply wish to record the
XML data that has been received, without performing much processing on the
data.
The following list describes techniques for handling invalid documents.
!
Reject the document and issue an error message
This is the most straightforward way of dealing with invalid documents.
Return an error message to the application that generated the document,
stating the reason why validation failed and the location of the problem
within the XML document.
This approach is suitable in situations where the creator of the invalid XML
document can respond to the error message, rectify the problem, and issue a
valid XML document instead.
!

Use the DOM to access the valid parts of the document
When an XML document fails validation, it does not necessarily imply that
the document is worthless. It might be the case that the document contains
enough valid content to meet your immediate needs.
For example, imagine that a bookstore submits an XML document to a book
publisher, requesting a delivery of books. The XML schema might require
essential information such as ISBN numbers, quantities, and the bookstore
delivery address. The XML schema might also require additional
information, such as the bookstore’s e-mail address for confirmation
messages.
Slide Objective
To provide some options in
case an invalid XML
document is detected.
Lead-in
Testing the validity of an
XML document is one thing.
What do you do if you find
that the document is invalid?
14 Module 8: Validating XML Data Using Schemas


Consider what should happen if the XML document inadvertently omits the
e-mail address. The publisher can still process the order by using DOM to
access the essential information in the XML document. The confirmation
message can be sent by post rather than by e-mail.
!
Use XSL to transform the document into a valid format
Rather than using DOM to access valid pockets of data within an invalid
XML document, you can apply an XSL style sheet to transform one version

of a document into another version.
For example, in the bookstore scenario described earlier, you can define a
style sheet that creates an empty e-mail address element and adds it to the
document.
Another use of style sheets in this context is to strip out repeating elements
where only a single instance is required. For example, the XML document
sent from the bookstore to the publisher might itemize all the authors for
each book. If the publisher only expects a single author per book, an XSL
style sheet can be applied in order to extract the first author and ignore the
others.
!
Ignore the validation error
There are many reasons for exchanging XML documents between a client
and server, such as placing orders, issuing confirmation messages,
exchanging technical information, and so on. In some situations, you might
not need to perform detailed processing on the XML data.
For example, a popular Web site might invite visitors to add their name and
address to the Visitors Book. In this case the Web site doesn’t perform any
significant processing on the data, so it doesn’t really matter if the data
format is slightly incorrect.
In XML terms, the way to ignore a validation error is to create an
XMLDOM object, disable validation, and load the document into the parser
programmatically. You will learn how to do this later in this module.

Module 8: Validating XML Data Using Schemas 15


#
##
#


Writing an XML Schema
!
Using a Microsoft XML Schema
!
Defining Element Types
!
Using Element Types
!
Defining Element Groups
!
Practice: Creating an XML Schema
!
Defining Attribute Types
!
Using Attribute Types
!
Demonstration: Writing an XML Schema


In this section, you will learn how to write a Microsoft
®
XML schema
document and apply it to a static XML document.
XML schema elements
An XML schema document may contain eight different types of elements,
which define the allowed syntax and structure for a particular XML grammar.
You must define these elements before they can be used.
Element Description


<Schema> Is the root element of the XML schema document.
<ElementType> Defines an element type that may be used within the XML schema
document.
<AttributeType> Defines an attribute type that may be used within the XML
schema document.
<element> Appears within <ElementType>, to define the allowed child
elements for that <ElementType>.
<group> Appears within <ElementType>, to define how child elements are
grouped within that <ElementType>.
<attribute> Appears within <ElementType>, to define the allowed attributes
for that <ElementType>.
<datatype> Appears within <ElementType> or <AttributeType>, to define the
data type for that <ElementType> or <AttributeType>.
<description> Provides documentation about <Schema>, <ElementType>, or
<AttributeType> elements.

Slide Objective
To provide an overview of
the topics in this section.
Lead-in
This section describes the
detailed syntax for creating
Microsoft XML schemas,
and shows how to apply
them to static XML
documents.
Delivery Tip
The company Extensibility
has a product called XML
Authority that can create an

XML schema from an XML
document. Go to

for more information.
16 Module 8: Validating XML Data Using Schemas



XML schemas are not yet finalized by the W3C. Microsoft has defined a
version of XML schemas known as XDR (XML Data Reduced) Schemas.
When the W3C finalizes what will be in an XML schema, Microsoft will help
customers migrate from XDR schemas to XML schemas.
In this module, all use of the term “XML schema” refers to the Microsoft XDR
Schema definition.

Note
Module 8: Validating XML Data Using Schemas 17


Using a Microsoft XML Schema
!
Create the XML schema
!
Apply the XML schema to a static XML document
<?xml version="1.0"?>
<Schema name="mySchema"
xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
</Schema>
<?xml version="1.0"?>

<Schema name="mySchema"
xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
</Schema>
<?xml version="1.0"?>
<booklist xmlns="x-schema:
/></booklist>
<?xml version="1.0"?>
<booklist xmlns="x-schema:
/></booklist>


To use an XML schema to validate an XML document, first create the XML
schema, and then apply it to the XML document.
Create the XML schema
An XML schema is an XML document. The Microsoft definition of XML
schemas requires a root element named <Schema>. The <Schema> element
contains all the rules defined in the XML schema document.
The <Schema> element requires a name attribute that defines the name of the
schema. Unlike DTDs, where the name of the DTD must match the root
element of the XML document, the name attribute in an XML schema may be
assigned any value:
<Schema name="mySchema" ... >
</Schema>

The <Schema> element must be defined with the following namespace
declaration. It is convenient to make this the default namespace in order to
avoid having to use a different namespace prefix throughout the XML schema
document:
xmlns="urn:schemas-microsoft-com:xml-data"


The <Schema> element should also contain namespace declarations for any
other schemas used, such as the namespace that defines the built-in data types
for XML schema:
xmlns:dt="urn:schemas-microsoft-com:datatypes"

Slide Objective
To introduce the overall
format of a Microsoft XML
schema document and
show how to apply one to an
XML document.
Lead-in
XML schemas are
themselves XML
documents. The Microsoft
definition for XML schemas
requires a root element
named <Schema>. To apply
the XML schema to an XML
document, set the
namespace of the root
element to the XML
schema.
Delivery Tip
These are Microsoft
schemas and not the W3C
standard.
Delivery Tip
Make sure students can

remember namespaces
from earlier in the course.
Remind them about
namespaces if necessary —
the concepts and the
syntax.
18 Module 8: Validating XML Data Using Schemas


The following example is a minimal XML schema document:
<?xml version="1.0"?>
<Schema name="mySchema"
xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
</Schema>

Applying the XML schema to a static document
To apply an XML schema to a static XML document, you must add a
namespace declaration of the following form to your XML document:
<?xml version="1.0" ?>
<myElement xmlns="x-schema:
</myElement>

The namespace declaration indicates the URL of the appropriate XML schema
document. The URL is prefixed by “x-schema” to indicate to the parser that the
URL defines an XML schema.
When the XML document is loaded into a validating XML parser, the parser
loads the XML schema so that validation may take place.
Notice that the XML schema is applied to the root element of the XML
document, for example, <myelement>, so that it defines the grammar for the

entire document. It is possible to apply an XML schema to any element in an
XML document, but applying it to the root element is the most common usage.

The parser in Internet Explorer 5.0 does not validate XML documents by
default. If you wish validation to take place, you can use DOM to load the
XML document programmatically, with validation enabled.

Note
Module 8: Validating XML Data Using Schemas 19


Defining Element Types
!
Use <ElementType> to define new element types
<ElementType
name = "element tag name"
content = "empty"|"textOnly"|"eltOnly"|"mixed"
model = "open" | "closed"
order = "one" | "seq" | "many"
dt:type = "XML data type" >
</ElementType>
<ElementType
name = "element tag name"
content = "empty"|"textOnly"|"eltOnly"|"mixed"
model = "open" | "closed"
order = "one" | "seq" | "many"
dt:type = "XML data type" >
</ElementType>
<ElementType name="book" content="eltOnly">
</ElementType>

<ElementType name="book" content="eltOnly">
</ElementType>


<ElementType> introduces a new type of element to the XML schema, and
defines the rules for that element when it appears in the XML document.
The XML schema must define a separate <ElementType> for each type of
element that can appear in the XML document (unless an “open” content model
is defined). You can specify an “open” element by setting the model attribute to
open.
<ElementType
name = "element tag name"
content = "empty"|"textOnly"|"eltOnly"|"mixed"
model = "open" | "closed"
order = "one" | "seq" | "many"
dt:type = "XML data type" >
</ElementType>

<ElementType> has the attributes name, content, model, order, and dt:type.
!
name
The name attribute must be provided. This attribute defines the tag name for
the <ElementType>, and therefore defines a valid tag name for elements in
the XML document. For example, if the <ElementType> name is “book”,
the XML document will have elements named <book>.
!
content
The content attribute is optional. This attribute defines the content type for
the element in the XML document.
Slide Objective

To describe how to define
element types in an XML
schema.
Lead-in
The XML schema must
define all element types
allowed in the XML
document.
Delivery Tip
In the syntax, the items
shown in bold are the
default values for the
element types.
Syntax

×