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: Rea
Trang 1Contents
Overview 1
Lesson: Validating XML While Parsing 7
Lesson: Advanced Validation 23
Review 32
Module 3:
Validating XML
Trang 2Information 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
Trang 3Instructor 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
Trang 4In 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
Trang 5How 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
Validation Event Handler
How to Get Details
About Validation Errors
Trang 6This 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
Microsoft Official Curriculum (MOC) courseware
Trang 7Overview
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
Trang 8Lesson: 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 objectives
Trang 9Overview of Validation
What is XML validation?
a set of requirements
Well-formed XML is a validation prerequisite
Why validate?
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?
Trang 10The 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
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
Trang 11What 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
***************************** 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 types
Trang 12In 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
<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"/>
Trang 13Lesson: 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 objectives
Trang 14***************************** 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
Trang 15To 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
Trang 16Introduction 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:
Introduction
Validation types
The ValidationType
property
Trang 17How to Instantiate a Validating Reader
***************************** 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);
Trang 18How to Associate Schemas with a Validating Reader
<books xmlns="urn:books-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:books-schema books.xsd">
<books xmlns="urn:books-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:books-schema books.xsd">
Associate schemas statically
Set the schemaLocation in the XML document
Or dynamically
Use XmlSchemaCollection
The XmlSchemaCollection object caches one or more schemas, which can improve performance
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
After instantiating the XML validating reader, you associate it with a literal XML schema or an XML schema collection by means of a namespace URI Each XML schema is associated with a namespace URI The
XmlValidatingReader can use the XSD schemaLocation attribute in the data
file that is bound to the XML schema instance namespace, http://www.w3.org/2001/XMLSchema-instance
If the schema is stored in an XmlSchemaCollection, the XmlValidatingReader uses the namespace URI specified when the schema
was added to the collection This namespace URI is typically the target namespace of the schema
To identify an XML schema, you set the schemaLocation attribute of the
validating reader object For example, the following example shows the root element of the data file:
<books xmlns="urn:books-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:books-schema books.xsd">
For this example, the value of the targetNamespace attribute is
urn:bookstore-schema, which is the same namespace that is used when adding the schema to
the XmlSchemaCollection
Introduction
How to identify a
schema
Trang 19You can use the XmlValidatingReader to validate XML documents against
multiple schemas
The XmlSchemaCollection is a cache or library where XDR and XSD schemas can be stored and validated The XmlSchemaCollection improves performance
by caching schemas in memory instead of accessing them from a file or URL
Although the XmlSchemaCollection class stores both XDR and XML schemas, any method and property that takes or returns an XmlSchema object
supports XSD schemas only
To load a schema into an XML collection object, you use the Add() method of XmlSchemaCollection The Add() method associates the schema that you
identify with a namespace URI For XML schemas, the namespace URI will typically be the target namespace for the schema For XDR schemas, the namespace URI is the namespace specified when the schema was added to the collection
You generally use the targetNamespace attribute when adding the namespaceURI property into the Add() method for the
XmlSchemaCollection
The XmlSchemaCollection can have only one schema without a namespace You should use a null (C#) or Nothing (Microsoft Visual Basic®) value for schemas without a namespace
To validate an XML instance document by using an XmlSchemaCollection, you first create the XmlSchemaCollection object, and then add your schemas
to the collection You then set the Schemas property on the XmlValidatingReader to assign the created XmlSchemaCollection to the XmlValidatingReader
If you are validating more than one document against the same schema, you
should use the XmlSchemaCollection because it provides better performance
by caching schemas in memory The XmlSchemaCollection improves
performance by storing schemas in the collection so that they are not loaded into memory each time validation occurs If the schema exists in the schema
collection, the schemaLocation attribute locates the schema in the collection The following code example creates the XmlSchemaCollection object, adds schemas to the collection, and sets the Schemas property:
' Visual Basic Dim BooksReader As New XmlTextReader("books.xml") Dim BooksValidater As New XmlValidatingReader(BooksReader) Dim BooksSchemas As New XmlSchemaCollection()
BooksSchemas.Add("urn:books-schema", "books.xsd") BooksValidater.Schemas.Add(BooksSchemas)
How to add the
collection to the reader
Performance benefits
Example
Trang 20How to Create a Validation Event Handler
Without a validation event handler, the first XmlException will cause parsing to stop
Validation may occur during a call to any of the Read() methods
Read(), ReadInnerXml(), ReadOuterXml(), Skip()
Set the reader validation type to:
DTD, Schema, XDR, or Auto
Create a method that responds to a ValidationEventHandler event
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
You use the ValidationEventHandler event to set an event handler for
receiving information about DTD, XDR, and XSD schema validation errors and warnings
Validation may take place during a Read(), ReadInnerXml(), ReadOuterXml(), or Skip() call At such times, the validation events happen only if the ValidationType property of XmlValidatingReader is set to DTD, Schema, XDR, or Auto
If a given element or attribute reports a validity error through the
ValidationCallback method because the content model does not match, the
rest of the content model for that element is not validated (However, the child elements of the given element or attribute are validated.) After the
XmlValidatingReader identifies an error for a given element, it stops
validating that element
For example, if the element named cookbook should be named recipebook, the element and its attributes will not continue to be validated, but its child elements (title, price) will be
<cookbook isbn="2" genre="fish">
Trang 21If no ValidationEventHandler is supplied and a parse error occurs, the error is reported by raising an XmlException for the first validation error with
XmlSeverityType = Error Exceptions are not raised for validation errors with XmlSeverityType = Warning; for example, if the schema file is not found If a
validation error occurs, an XmlSchemaException is thrown If an exception is thrown, the XmlValidatingReader cannot be restarted
The following code example creates a ValidationCallback method that is
attached to the event handler of the reader:
' Visual Basic AddHandler BooksValidater.ValidationEventHandler, _ AddressOf Me.ValidationCallback
Private Sub ValidationCallback(ByVal sender As Object, _ ByVal e As ValidationEventArgs)
End Sub // C#
BooksValidater.ValidationEventHandler +=
new ValidationEventHandler(this.ValidationCallback);
private void ValidationCallback(
object sender, ValidationEventArgs e) { }
If there is no event
handler
Example
Trang 22How to Get Details About Validation Errors
ValidationEventArgs is a parameter of ValidationEventHandler
ValidationEventArgs has the following properties:
XmlSchemaException has the following properties:
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
The ValidationEventArgs class provides several ways for you to obtain details
about validation errors
There are two general types of errors: fatal and nonfatal Fatal errors happen
when the validating reader finds a validation error in the instance document For
fatal validation errors, the Severity property assumes the value of XmlSeverityType.Error
An example of a nonfatal error is when a schema cannot be found For all
nonfatal validation errors, the Severity property assumes the value XmlSeverityType.Warning Warnings can occur for all ValidationType values
except ValidationType.None
The Message property contains a string describing the error The Exception
property contains useful information such as LineNumber, LinePosition, and SourceUri
Introduction
Types of errors
The Message property