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

Tài liệu Module 3: Validating XML ppt

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 (926.16 KB, 30 trang )









Contents
Overview 1
Lesson: Introducing Validation 2
Lesson: Using XSD Schemas 11
Quiz 15
Lab 3: Using Office XP to Generate a
Schema 16

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, places or events 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.

 2001 Microsoft Corporation. All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BackOffice, bCentral, BizTalk,
FrontPage, MSDN, MSN, Netshow, PowerPoint, SharePoint, Visio, Visual Basic, Visual C++,
Visual C#, Visual InterDev, Visual Studio, Windows Media, and Xbox are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. 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, participants will be able to:
!
Identify situations where validation is a requirement.
!
Distinguish validation documents that conform to interim schema standards
from those that conform to the current schema standard.
!
Use Microsoft
®
Office XP to create a schema.
!

Use a schema to validate an instance document.

Required Materials and Preparation Tasks
To teach this module, you need the following Microsoft PowerPoint
®
files:
!
2500A_03.ppt
!
2500A_03PopQuiz.ppt

To prepare for this module:
!
Read all of the materials for this module.
!
Complete all practices.
!
Complete the demonstrations.
!
Complete the lab.
!
Be ready to explain several situations that require Extensible Markup
Language (XML) validation.
!
Be ready to explain the difference between a document type definition
(DTD), an XML-Data Reduced (XDR) schema, and an XML Schema
Definitions (XSD) schema.
!
Be ready to describe the World Wide Web Consortium (W3C) schema data
types, and attribute and element declarations.


Read the W3C XML Schema Definition Primer document and the Microsoft
MSDN
®
Online topic on XSD schemas.
Presentation:
45 Minutes

Lab:
20 Minutes
Required Materials
Preparation Tasks
Recommended Readin
g
iv Module 3: Validating XML


Module Strategy
This module contains two lessons and one lab.
!
Introducing Validation
In this lesson, participants learn what validation means. This lesson defines
validation and describes its range of application. Participants learn how to
identify DTDs and XDR schemas. One of the objectives of this lesson is
for participants to be able to recognize DTDs and XDR schemas, and to a
lesser extent read them. Participants do not need to know how to generate a
DTD or an XDR schema, only to recognize one and to a limited extent, to
read one.
Emphasize that DTD and XDR are earlier technologies. Spend more time on
the second lesson on XSD.

!
Using XSD Schemas
In this lesson, participants learn the parts of an XSD schema and how to
generate one from a Microsoft Access 2002 table. The concept of data types
is restricted to discussing two main categories of data types, simple and
complex types. The syntax of declaring attribute and element names is
discussed.
At the close of this lesson is a quiz that presents participants with some
example code. To present this quiz, open 2500A_03PopQuiz.ppt. The
question and code sample appear first. When you are ready, click the
slide and the answer appears. In the quiz, participants identify validation
requirements and the type of schema that is being presented.

Module 3: Validating XML 1


Overview
!
Introducing Validation
!
Using XSD Schemas

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
What happens when an application receives a command to process an
Extensible Markup Language (XML) document that, although well-formed, is
composed according to a vocabulary different from the one that the application

is programmed to process? One might expect a variety of errors to occur. The
purpose of validating XML is to prevent processing errors at the level of the
application.
After completing this module, you will be able to:
!
Identify situations where validation is a requirement.
!
Distinguish validation documents that conform to interim schema standards
from those that conform to the current schema standard.
!
Use Microsoft
®
Office XP to create a schema.
!
Use a schema to validate an instance document.

Introduction
Objectives
2 Module 3: Validating XML


Lesson: Introducing Validation
!
What Is Valid XML?
!
Validation Scenarios
!
Evolution of Validation Technologies
!
How to Recognize a Document Type Definition

!
How to Recognize an XDR Schema

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Validation is fundamental to everything else you do with XML. The standards
for validating have changed dramatically over the last three years.
After completing this lesson, you will be able to:
!
Define XML validation.
!
Identify the places in a distributed computing scenario where XML
validation is a requirement.
!
Describe the evolution of XML validation.
!
Identify a document type definition (DTD).
!
Differentiate between an XML-Data Reduced (XDR) schema and an XML
Schema Definitions (XSD) schema.

Introduction
Lesson ob
jectives
Module 3: Validating XML 3



What Is Valid XML?
<orders>my data</orders>
<orders>my data</orders>
Valid XML Not Valid XML
<Orders>my data</Orders>
<Orders>my data</Orders>
Validation
XML Processor
Application
Error!
Root element must be lower case.
Error!
Root element must be lower case.
Both documents are well-formed but…
A schema is a specification for XML structure and data types
A schema is a specification for XML structure and data types
Only the valid document reaches the application for further processing

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
For an XML document to be exchanged between applications, it must be
well-formed and valid.
XML is valid if its vocabulary conforms to a set of requirements, or schema.
In computing, a schema is a general term for a description of a set of data. In
XML, a schema is a description of an XML document. You use a schema to
validate XML documents. An XML document that you validate with a schema
is called an instance document. If an instance document matches the schema

definition, the instance document is said to be valid.
There are three types of schemas for validating an XML instance document.
Type of schema Description

DTD DTD is the original validation method described in W3C XML
Recommendation version 1.0. XSD superceded the DTD. DTDs
are not based on XML.
XDR schema XDR is an interim schema technology developed by Microsoft.
XDR is similar to XSD. XDR schemas are written in XML.
XSD schema XSD is the W3C Recommendation for validating XML
schemas. It replaces both DTDs and XDR schemas. XSD
schemas are written in XML.

Introduction
Definition
Validation terminology
and methods
4 Module 3: Validating XML


Validation Scenarios
!
Data transferred between systems requires validation
Organization A
Organization A
Client
Client
1. Validate user input prior to uploading.
2. Validate incoming XML against a required vocabulary.
3. Validate details against business logic prior to processing.

Organization B
Organization B
1
1
1
2
2
2
3
3
3
2
2
2

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Validation is a requirement for transferring data between systems that span
from one system to another or from one organization to another.
Supply chain management is an example where validation is necessary. A
supply chain can consist of many business partners, each passing data from one
to the next. Each business partner may pass data between its organizational
units. Validation ensures that data passed from one point to the next, within and
between organizations, is useful.
There are three places in a data exchange process where you typically find
validation.
Point in the process What to validate


Client A client can validate the format and completeness
of data before sending it to a Web server.
Between partners A business partner can validate incoming data
against a schema before processing it further.
Within an application An application can validate content against business
logic before processing it further.

There are costs to validating.
!
Validation can slow application performance.
!
Time is required to develop and manage schemas.
!
If you validate data, then you must also develop error handling in case of
invalid data.

Not all situations require validation. If the XML vocabulary, data types, and
data formats contained in the XML you plan to process reliably conform to the
requirements of your system, then you might not need to validate the XML. For
example, an internal application that consumes its data from sources you
control might not require validation.
Introduction
When to validate
When not to validate
Module 3: Validating XML 5


How to Recognize a Document Type Definition
!

DTDs are superceded by XSD schemas, but you might still need to
work around them
!
A reference to
an external DTD
!
An external DTD
!
An inline DTD
<!ELEMENT bookstore (book)*>
<!ELEMENT book (title,author*,price)>
<!ATTLIST book genre CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (name | (first-name,last-name))>
<!ELEMENT price (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT last-name (#PCDATA)>
<!ELEMENT bookstore (book)*>
<!ELEMENT book (title,author*,price)>
<!ATTLIST book genre CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (name | (first-name,last-name))>
<!ELEMENT price (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT last-name (#PCDATA)>
<?xml version='1.0'?><!DOCTYPE bookstore
SYSTEM "books.dtd">
<?xml version='1.0'?><!DOCTYPE bookstore

SYSTEM "books.dtd">
<?xml version='1.0'?>
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book)*>
<?xml version='1.0'?>
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book)*>
DTD Example

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Although DTDs have been superceded by XSD schemas, you may still
encounter a DTD in earlier code. Therefore, you should be able to read and
understand a DTD.
DTD validation is defined in the World Wide Web Consortium (W3C)
XML 1.0 Recommendation.
A DTD is a schema based on Standard Generalized Markup Language (SGML).
You use DTDs to specify the content and values that are allowed for an XML
document. DTDs use a formal vocabulary to describe the structure and syntax
of XML documents. The DTD may be located in a file external to the XML
document, or it may be inline, located within the XML document.
The <!DOCTYPE> declaration is required to reference a DTD. You can
determine if an XML document is validated against a DTD if a DTD is
referenced within a <!DOCTYPE> declaration.
The name of the root element of the XML file is also specified in the
<!DOCTYPE> declaration. A system keyword or public keyword follows. If
the SYSTEM keyword is used, the location value must be set to a valid

Uniform Resource Locator (URL). If the PUBLIC keyword is used, the location
value is simply passed to whatever application is using the DTD for the
application to resolve.
The following is the syntax for the <!DOCTYPE> declaration:
<!DOCTYPE RootElementName [SYSTEM|PUBLIC] "Location">

Introduction
Definition
How to recognize a
reference to an external
DTD
Syntax
6 Module 3: Validating XML


In the following example, an instance document is validated against an external
DTD:
<?xml version="1.0"?>
<!DOCTYPE StockList SYSTEM "List.dtd">
<StockList>
<Item Quant=’1000’ SKU=’13’>Thermometer</Item>
<Item Quant=’10’ SKU=’45’>Flashlight</Item>
<Item Quant=’4’ SKU=’23’>Timer</Item>
<Item Quant=’1’ SKU=’98’>Plastic Tray</Item>
</StockList>

Notice the following:
!
<StockList> and all child elements are validated against the DTD that the
SYSTEM keyword references.

!
The SYSTEM keyword indicates that List.dtd is a literal URL reference.

Here is an example of an external DTD with the file name List.dtd:
<!ELEMENT StockList (Item)*>
<!ATTLIST Item Quant CDATA #REQUIRED>
<!ATTLIST Name SKU CDATA #REQUIRED>

Notice the following:
!
It is not an XML document in form or name.
!
It specifies that the root element must be named <StockList>.
!
Any number of child elements called <Item> may be present.
!
Each <Item> element requires value declarations for the attributes Quant
and SKU.

Example of a reference
to an external DTD
Example of an external
DTD
Module 3: Validating XML 7


Sometimes the DTD is supplied within the XML document that it validates.
The DTD is referenced just after the XML declaration, as follows:
<?xml version='1.0'?>
<!DOCTYPE bookstore [

<!ELEMENT bookstore (book)*>
<!ELEMENT book (title,author*,price)>
<!ATTLIST book genre CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (name | (first-name,last-name))>
<!ELEMENT price (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT last-name (#PCDATA)>
]>
<!-- This represents a fragment of a book store inventory
database -->
<bookstore>
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>


The DTD, as an XML validation technology, is being replaced by the W3C
XSD Recommendation. Projects that are currently deployed that use DTDs may
need to be updated. There are many DTD-to-schema conversion utilities
available.
How to recognize a
reference to an inline
DTD
DTD-to-schema
conversion
8 Module 3: Validating XML


How to Recognize an XDR Schema
!
External XDR schema
!
Inline XDR schema
!
Reference to an XDR schema in a source document
<Schema name="MySchema" xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<Schema name="MySchema" xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<root xmlns:data="x-schema:#inLineSchema">
<Schema name ="inLineSchema“ xmlns="urn:schemas-microsoft-com:xml-data">
<ElementType name="myelement"/>
</Schema>
<data:myelement>inline example</data:myelement>
</root>

<root xmlns:data="x-schema:#inLineSchema">
<Schema name ="inLineSchema“ xmlns="urn:schemas-microsoft-com:xml-data">
<ElementType name="myelement"/>
</Schema>
<data:myelement>inline example</data:myelement>
</root>
<ElementName xmlns="x-schema:yourschema.xml"> </ElementName>
<ElementName xmlns="x-schema:yourschema.xml"> </ElementName>
XDR Schema Example

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
XSD is an interim technology put forth by Microsoft in anticipation of the W3C
XSD Schema Recommendation. Although XSD has reached W3C
Recommendation status, making XDR obsolete, you may still see many
examples of XDR in use.
Like DTDs, XDR schemas have been used to specify the schema, or structure,
of a particular class of documents.
An XML file is an XDR schema when:
!
The file extension is .xdr or .xml.
!
The root element is named <Schema> or <schema>.
!
There are references to Microsoft schema namespaces.

The following is an example of an XDR schema named Play.xml:

<?xml version="1.0"?>
<Schema name="schema_sample_1"
xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="PERSONA" content="textOnly"
model="closed"/>
<ElementType name="GRPDESCR" content="textOnly"
model="closed"/>
<ElementType name="PGROUP" content="eltOnly" model="closed">
<element type="PERSONA" minOccurs="1" maxOccurs="*"/>
<element type="GRPDESCR" minOccurs="1" maxOccurs="1"/>
</ElementType>
</Schema>

Introduction
How to recognize an
XDR schema
Example

×