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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 http://www.w3.org . ! Read the latest information about Microsoft’s support for XML schemas at http://msdn.microsoft.com/xml . 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 http://msdn.microsoft.com . ! 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>jdoe@microsoft.com</email-address> <postal-address>12 Main Street</postal-address> <email-address>jdoe@microsoft.com</email-address> <address type="postal">12 Main Street</address> <address type="email">jdoe@microsoft.com</address> <address type="postal">12 Main Street</address> <address type="email">jdoe@microsoft.com</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>jdoe@microsoft.com</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. [...]... throughout the XML schema document: xmlns="urn :schemas- microsoft-com :xml- data" The 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" 17 18 Module 8: Validating XML Data Using Schemas The following example is a minimal XML schema document: < ?xml version="1.0"?>... 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 Module 8: Validating XML Data Using Schemas Using a Microsoft XML Schema 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. .. xmlns="urn :schemas- microsoft-com :xml- data" xmlns="urn :schemas- microsoft-com :xml- data" xmlns:dt="urn :schemas- microsoft-com:datatypes"> xmlns:dt="urn :schemas- microsoft-com:datatypes"> ! Apply the XML schema to a static XML document < ?xml version="1.0"?> < ?xml version="1.0"?> ... an XML document 5 6 Module 8: Validating XML Data Using Schemas Detecting Incorrect Documents Using Validation 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 ! Defining an XML schema or DTD $ ! Specify the structure for a class of XML documents Applying XML schemas. .. follows: dtd2schema -o myxmlfile .xml mydtdfile.dtd This creates an XML schema named myxmlfile .xml Module 8: Validating XML Data Using Schemas 11 Causes of Invalid XML Documents Slide Objective To describe some of the situations that give rise to invalid XML documents ! XML mismatches can occur between the client and server ! Reasons Lead-in There are several reasons why invalid XML documents get created... 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 Module 8: Validating XML Data Using Schemas 9 XML Schemas vs DTDs Slide Objective To describe the technical advantages of XML schemas as compared to DTDs ! XML schemas $ Are extensible... Appears within or , to define the data type for that or Provides documentation about , , or elements 16 Module 8: Validating XML Data Using Schemas Note XML schemas are not yet finalized by the W3C Microsoft has defined a version of XML schemas known as XDR (XML Data Reduced) Schemas. .. definition Only one DTD document can be attached per XML document 10 Module 8: Validating XML Data Using Schemas Delivery Tip ! 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... 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" ?> The namespace... to the Web server Module 8: Validating XML Data Using Schemas 13 Handling Invalid Documents 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? If an XML document is found to be invalid, several options are available: $ Reject the document and issue . 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 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

Ngày đăng: 10/12/2013, 16:16

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan