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

Module 3: Validating XML

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 (922.55 KB, 44 trang )









Contents
Overview 1
Lesson: Examining Schemas 2
Lesson: Validating XML While Parsing 7
Lesson: Advanced Validation 23
Review 32
Lab 3.1: Validating XML 34

Module 3:
Validating XML



Information in this document, including URL and other Internet Web site references, is subject to
change without notice. Unless otherwise noted, the example companies, organizations, products,
domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address,
logo, person, place or event is intended or should be inferred. Complying with all applicable
copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part
of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted
in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or
for any purpose, without the express written permission of Microsoft Corporation.


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.

©2002 Microsoft Corporation. All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, Win32, Active Directory, ActiveX, BizTalk,
IntelliSense, JScript, Microsoft Press, MSDN, PowerPoint, SQL Server, Visual Basic, Visual C#,
and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the
United States and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.



Module 3: Validating XML iii

Instructor Notes
After completing this module, students will be able to:

Instantiate an XML validating reader.

Associate one or more XML schemas with an XML validating reader.

Handle errors that result when a validating reader processes invalid XML.

Create an application that uses the Microsoft
®

.NET Framework XML
classes and XML schemas to validate an XML fragment.

Resolve a Uniform Resource Identifier (URI) reference to a schema or
Document Type Definition (DTD).

Create an application that uses the .NET Framework XML classes and an
XML Schema Definition (XSD) schema or a DTD to validate well-formed
XML.

To teach this module, you need the following materials:

Microsoft PowerPoint
®
file 2663A_03.ppt

Code samples file 2663A_03_Code.htm

To prepare to effectively teach this module:

Read the following Microsoft Visual Studio
®
Help topics:
• Walkthrough: Creating an XML File with an Associated XML Schema
• XML Schemas and Data
• XML Data Types Reference
• Authoring and Using Custom Schemas Backgrounder
• Validation of XML with Schemas

Read the entire module.


Complete the practices and the lab.

Practice delivering the demonstrations.

Set Visual Studio .NET display options so that XML and HTML will
display for optimum visibility in the classroom.


Change the display options
1. On the Tools menu, click Options.
2. Click the Text Editor folder, and then click the HTML/XML folder.
3. Select the Word wrap and Line numbers options.
4. Click the Environment folder, and then click the Fonts and Colors folder.
5. Change the font used for the Text Editor and Text Output Tool windows to
Lucida Console typeface 14 point.
6. Click OK.
7. Close and restart Visual Studio .NET.

Presentation:
45 minutes

Lab:
45 minutes
Required materials
Preparation tasks
iv Module 3: Validating XML


In this module, some of the Microsoft PowerPoint

®
slides provide hyperlinks
that open a code samples page in the Web browser. The code samples page
provides a way to show and discuss code samples when there is not enough
space for the code on the PowerPoint slide. It also allows students to copy code
samples directly from the browser window and paste them into a development
environment. All of the linked code samples for this module are in a single .htm
file.
To open a code sample, click the appropriate hyperlink on the slide. To navigate
between code samples in a particular language, use the table of contents
provided at the top of the code page. Each hyperlink opens a separate instance
the Web browser, so it is a good practice to click Back in Microsoft
Internet Explorer after viewing a code sample. This will close the browser
window and return you to the PowerPoint presentation.
Hyperlinked Code
Examples
Module 3: Validating XML v

How to Teach This Module
This section contains information that will help you to teach this module.
Lesson: Examining Schemas
This section describes the instructional methods for teaching each topic in this
lesson.
The goal of this topic is to reacquaint learners with the basics of validation.
Remind learners that the .NET Framework provides classes to validate XML by
using DTDs or XML schemas.
Quickly provide a definition of the XSD schema.

Lesson: Validating XML While Parsing
This section describes the instructional methods for teaching each topic in this

lesson.
This demonstration is based on a custom add-in. Demonstrate how to use this
add-in to validate XML by using an XSD schema, and then do the same by
using a DTD. Point out how both operations use the XmlValidatingReader
class. In the code examinations that follow, you will have the opportunity to
explain in greater detail how the underlying code works.
Introduce the XmlValidatingReader class by pointing out that it is derived
from the XmlReader class. Next, outline the four major programming tasks
that are required to use an XmlValidatingReader. Use the programming tasks,
listed in the slide and below, to provide learners with an overview of the coding
required to create a simple validating reader. The following tasks are covered in
the next four topics:
1. Instantiate a validating reader.
2. Associate one or more schemas with the validating reader.
3. Create a validation event handler.
4. Process nodes by calling one of the Read() methods.

Show the code that instantiates a validating reader.

Show the code that allows you to associate one or more schema with a
validating reader.

Show the code that implements an error-handling routine.

Show the code that allows you to poll data from validation errors.

Overview of Validation
What Is an XML
Schema?
Demonstration:

Validatin
g XML
Introduction to the
XmlValidatingReader
Class
How to Instantiate a
Validatin
g Reader
How to Associate
Schemas with a
Validating Reade
r
How to Create a
Validation Event Handler
How to Get Details
About Validation Errors
vi Module 3: Validating XML


This code examination shows learners how an add-in works. More importantly,
it shows the inner workings of a simple validation application. In the code
examination that follows this one, you will explain code that uses a validating
reader and a DTD.
This practice covers the four previously described coding tasks. Because these
tasks are central to creating a generic validating reader, the practice is
comprehensive in its scope and borders on the scale of a mini-lab. You should
therefore allow learners plenty of time and offer assistance to complete this
practice.
Lesson: Advanced Validation
This section describes the instructional methods for teaching each topic in this

lesson.
Show code that resolves a DTD resource used by XmlValidatingReader.

Show code that handles cases where only a fragment of XML must be
validated, instead of an entire XML document.
Show the code that validates XML by using a DTD instead of an XSD schema.

Explain the code that performs DTD validation on an XML document not
conforming to the DTD named by the validating reader.

Review
Several multiple-choice review questions appear at the end of the module.
Review these questions with your students before proceeding to the lab.
Customization Information
This section identifies the lab setup requirements for a module and the
configuration changes that occur on student computers during the labs. This
information is provided to assist you in replicating or customizing
Microsoft Official Curriculum (MOC) courseware.
Lab Setup
There are no lab setup requirements that affect replication or customization.
Lab Results
There are no configuration changes on student computers that affect replication
or customization.
Code Examination:
Validating XML Using an
XSD Schema
Practice: Validating XML
How to Resolve External
Resources
How to Validate XML

Fra
gments
How to Validate XML
Using a DTD
Code Examination:
Validating XML Using a
DTD
Module 3: Validating XML 1

Overview

Examining Schemas

Validating XML While Parsing

Advanced Validation

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
This module gives you an overview of XML Schema Definition (XSD) data
types and then covers how to use the Microsoft
®
.NET Framework Extensible
Markup Language (XML) classes to perform XML schema validation. The
lesson Advanced Validation teaches you how to resolve external entities, how
to validate XML fragments, and how to validate XML by using a Document
Type Definition (DTD).

After completing this module, you will be able to:

Instantiate an XML validating reader.

Associate one or more XML schemas with an XML validating reader.

Handle errors that result when a validating reader processes invalid XML.

Create an application that uses the .NET Framework XML classes and XML
schemas to validate an XML fragment.

Resolve a Uniform Resource Identifier (URI) reference to a schema or
DTD.

Create an application that uses the .NET Framework XML classes and an
XSD schema or a DTD to validate well-formed XML.

Introduction
Objectives
2 Module 3: Validating XML


Lesson: Examining Schemas

Overview of Validation

What Is an XML Schema?

*****************************
ILLEGAL FOR NON

-
TRAINER USE
******************************
The lesson provides an overview of validation and XML schema data types.
After completing this lesson, you will be able to:

Describe the relationship between valid XML and well-formed XML.

Describe why XML validation is important and when it should be used.

Explain the differences between the three categories of schema data types.

Describe how XML schemas validate instance documents.

Introduction
Lesson ob
jectives
Module 3: Validating XML 3

Overview of Validation

What is XML validation?

The process of comparing XML structure, names, and data types to
a set of requirements

A schema specifies these requirements

Well-formed XML is a validation prerequisite


Why validate?

To filter data shared between organizations and applications

To protect databases against invalid data types

To standardize data structures

The .NET Framework supports these validation technologies: XSD,
XDR, and DTD

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Validating XML is the process of comparing an XML source to a predefined set
of requirements. A prerequisite for validation is well-formedness. Validation
requirements are defined in terms of names of elements and attributes, the
nesting structure of elements, and the type of data that can occur in attributes
and elements.
An XML schema is a well-formed XML document that describes the XML
structure and data types of another XML document. An XML document that
conforms to the description of XML contained in a schema is said to be a valid
instance of that schema. You use a schema to ensure that XML adheres to a
specified structure and data type.
There are three major reasons to validate XML:

To filter data shared between organizations and applications


To protect databases against invalid data types

To standardize data structures

What is XML validation?
Why validate?
4 Module 3: Validating XML


The .NET Framework supports XSD schemas and, for backward compatibility,
DTD and XML-Data Reduced (XDR) schemas. These validation technologies
are described in the following table.
Validation technology Description

Document Type Definition (DTD) DTD is the first proposed World Wide
Web Consortium (W3C) schema
technology. Although you might still find
DTDs in use, they are considered an
obsolete technology because they are not
expressed in XML and because they
provide limited support for specifying data
types.
XML-Data Reduced (XDR) An interim Microsoft schema technology
based on XML.
XML Schema Definition (XSD) The current W3C standard, based on
XML.

.NET Framework
support for validation
technolo

gies
Module 3: Validating XML 5

What Is an XML Schema?

An XML schema is an XML document that describes a
second XML document

Describes instance documents in terms of data types

Built-in types define element content as integer, string,
date and time, and so on

Simple types define attributes and define elements that
have neither attributes nor child elements

Complex types define elements that may contain
attributes or elements

XML schemas use the XML schema namespace
Simple Type Example Complex Type Example

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
An XML schema is an XML document that describes a second XML document.
If the second XML document conforms to the XML described in the schema, it
is said to be a valid instance of the schema. You can use XML schemas to

ensure that only valid XML is passed through a data-processing system.
The three categories of data types defined in schema definitions are built-in
types, simple types, and complex types. Each data type allows you to validate a
different aspect of an XML source.
Built-in types allow you to specify the data that can appear as content. Built-in
types are similar to the data types that you typically encounter in most
programming languages. String, integer, and decimal are examples of built-in
types. There are many built-in types specialized to handle a wide variety of
content that is bound in XML elements. For example, some built-in types are
optimized to handle date and time information, others handle Uniform Resource
Locators (URLs), and many others are suited to handle a variety of specialized
strings.
Built-in types are themselves divided into primitive types and derived types.
Primitive types are the most basic form of data type. Integer and string are
examples of primitive types. Derived types are extensions of primitive types.
For example, many derived types are extensions of the primitive string type.
A simple type defines elements that do not contain child elements or attributes.
A simple type also defines attributes.
To restrict the kind of built-in type that can be stored in a simple type element
or attribute, you create a restriction element and assign the built-in type as the
value of its base attribute. If the built-in type is a string, you can further restrict
valid instances of the string by defining a regular expression. A regular
expression works similarly to an input mask.
Introduction
Built-in types
Simple t
ypes
6 Module 3: Validating XML



In the following example, the simpleType element specifies how an element
should be structured to contain a string. The string represents a stock-keeping
unit (SKU). The built-in type of the SKU element is assigned in the base
attribute. The pattern to which an SKU string must conform is defined by a
regular expression assigned to the value attribute.
<xsd:simpleType name="SimpleType_SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>

The following element is a valid instance of this simple type:
<SKU>123-AZ</SKU>

The following element is an invalid instance of this simple type because the
element contains an attribute. An element that contains an attribute is by
definition a complex type, not a simple type.
<SKU price='20'>123-AZ</SKU>

Complex types are used in a schema to define elements that are expected to
contain at least one child element or an attribute. For example, in most cases the
root element contains at least one child element and one or more attributes.
Complex types are defined by the complexType element.
The following is an example of a complex type definition. It specifies how an
element should be structured to contain a United States address. The
complexType element contains a name attribute whose value is assigned
USAddress. The elements and attributes of a valid instance are associated with
the built-in types. Except for the zip element, which is typed as a decimal, the
other address elements are typed as strings. The country attribute is typed as an
NMTOKEN type, which is an extension of the string primitive type.

<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
fixed="US"/>
</xsd:complexType>

For more information about XSD data types and facets, see the
.NET Framework General Reference.
Example of a simple
type
Complex types
Example of a complex
t
ype
Module 3: Validating XML 7

Lesson: Validating XML While Parsing

Demonstration: Validating XML

Introduction to the XmlValidatingReader Class

How to Instantiate a Validating Reader

How to Associate Schemas with a Validating Reader


How to Create a Validation Event Handler

How to Get Details About Validation Errors

Code Examination: Validating XML Using an
XSD Schema

Practice: Validating XML While Parsing

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In this lesson, you will learn how to validate XML while parsing. The ability to
read and validate XML simultaneously makes an application highly efficient.
After completing this lesson, you will be able to:

Compare the pull model of XML processing to the push model.

Instantiate a validating parser.

Associate a validating reader with one or more schemas.

Create a validation event handler.

Base error-handling routines on the details that the event handler provides.

Build a robust application that validates XML against multiple schemas

while it parses.

Introduction
Lesson ob
jectives
8 Module 3: Validating XML


Demonstration: Validating XML

Use an XmlValidatingReader to validate an
XML document

The XmlValidatingReader can use an XSD
schema or a DTD

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In this demonstration, you will see the XML validation functionality of the
XML Tools add-in. The add-in should already be installed and loaded. If not,
see Appendix A, “The XML Tools Add-In,” for instructions.

To validate sample XML files by using a sample XML schema
1. In Microsoft Visual Studio
®
.NET, open the files booksGood.xml,
booksBad.xml, and books.xsd.

2. On the XML Tools toolbar, click Validate (XSD).
3. Select the booksGood.xml file to validate by using books.xsd, and then click
OK. The file will successfully parse without errors.
4. On the XML Tools toolbar, click Validate (XSD).
5. Select the booksBad.xml file to validate by using books.xsd, and then click
OK. The file will parse, but with three errors.
6. In the Validation Errors dialog box, examine each of the errors by clicking
each error in the list and reading its description, and then click OK.
7. On the View menu, click Show Tasks, and then click All. Notice that the
three errors have been added as tasks.
8. Use the Microsoft Visual Studio
®
.NET code editor to fix the problems with
the booksBad.xml file.
9. Save the changes you made to the booksBad.xml file.
10. On the XML Tools toolbar, click Validate (XSD).
11. Select the booksBad.xml file to validate by using books.xsd, and then click
OK. If you fixed the errors, the file will successfully parse without errors.

Introduction
Demonstration
Module 3: Validating XML 9


To validate sample XML files by using a sample DTD
1. Open the files headcount.xml and headcount.dtd.
2. On the XML menu, click Validate XML Data. Notice that the
Visual Studio .NET development environment can only validate XML
documents using XML schemas, not DTDs.
3. On the XML Tools toolbar, click Validate (DTD).

4. Select the headcount.xml file and then click OK. The file will successfully
parse without errors.
5. Make a change to the headcount.xml file by using the Visual Studio .NET
code editor. For example, change the name of the First attribute to
FirstName for one of the elements.
<Name FirstName="Nancy" Last="Davolio">

6. Save the changes to the headcount.xml file.
7. On the XML Tools toolbar, click Validate (DTD).
8. Select the headcount.xml file, and then click OK. The file will parse, but
with two errors.
9. In the Validation Errors dialog box, examine each of the errors, and then
click OK.

10 Module 3: Validating XML


Introduction to the XmlValidatingReader Class

XmlValidatingReader inherits from XmlReader

For read-only, forward-only, non-cached parsing with
validation

Programming task summary:
1. Instantiate a validating reader.
2. Associate one or more schemas with the
validating reader.
3. Create a validation event handler.
4. Process nodes by calling one of the Read() methods.


*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
The XmlValidatingReader class is an implementation of the XmlReader class
that provides support for XML validation. You can use the
XmlValidatingReader class to validate XML documents and XML fragments.
This class implements the validity constraints defined in the W3C XML 1.0
Recommendation for DTDs, the Microsoft XML (MSXML) schema
specification for XDR schemas, and the W3C XML Schema Recommendation
for XSD schemas.
You can use the following three types of schemas to validate an XML
document by using the XmlValidatingReader:

DTD

XDR

XSD

You set the ValidationType property to specify the type of validation
performed by the XmlValidatingReader. You must set this property before
calling the Read() method.
Introduction
Validation t
ypes
The ValidationType
property

Module 3: Validating XML 11

How to Instantiate a Validating Reader
1. Create an XmlTextReader
2. Pass the XmlTextReader as a parameter to the
XmlValidatingReader constructor
3. Set the ValidationType property to:

Auto

Schema

XDR

DTD

None
Visual Basic Example C# Example

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Programming an application that validates XML requires several steps. The
first step is to instantiate a validating reader.
To instantiate a validating reader, you first construct a new XML text reader,
providing the XML source you intend to validate as an argument in the text
reader constructor. You then construct a new XML validating reader and supply
this instance with the name of the XML text reader.

You set the ValidationType property to specify the type of validation
performed by the XmlValidatingReader. You must set this property before
calling the Read() method.
In the following example, a new XML text reader is constructed and provided
with an XML file to load. This text reader is then loaded by a new XML
validating reader.
' Visual Basic
Dim BooksReader As New XmlTextReader("c:\books.xml")
Dim BooksValidater As New XmlValidatingReader(BooksReader)
BooksValidater.ValidationType = ValidationType.Schema

// C#
XmlTextReader BooksReader =
new XmlTextReader(@"c:\books.xml");
XmlValidatingReader BooksValidater =
new XmlValidatingReader(BooksReader);
BooksValidater.ValidationType = ValidationType.Schema;

Introduction
Instantiate a validating
reade
r
The ValidationType
prope
rty
Example

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×