Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 19 Extensible Markup Language (XML)

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 19 Extensible Markup Language (XML)"— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 19 Extensible Markup Language (XML)

2  2006 Pearson Education, Inc. All rights reserved. 2 Knowing trees, I understand the meaning of patience. Knowing grass, I can appreciate persistence. — Hal Borland Like everything metaphysical, the harmony between thought and reality is to be found in the grammar of the language. — Ludwig Wittgenstein

3  2006 Pearson Education, Inc. All rights reserved. 3 I played with an idea, and grew willful; tossed it into the air; transformed it; let it escape and recaptured it; made it iridescent with fancy, and winged it with paradox. — Oscar Wilde

4  2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  To mark up data using XML.  How XML namespaces help provide unique XML element and attribute names.  To create DTDs and schemas for specifying and validating the structure of an XML document.  To create and use simple XSL style sheets to render XML document data.  To retrieve and modify XML data programmatically using.NET Framework classes.  To validate XML documents against schemas using class XmlReader.  To transform XML documents into XHTML using class XslCompiledTransform.

5  2006 Pearson Education, Inc. All rights reserved. 5 19.1 Introduction 19.2 XML Basics 19.3 Structuring Data 19.4 XML Namespaces 19.5 Document Type Definitions (DTDs) 19.6 W3C XML Schema Documents 19.7 (Optional) Extensible Stylesheet Language and XSL Transformations 19.8 (Optional) Document Object Model (DOM) 19.9 (Optional) Schema Validation with Class XmlReader 19.10 (Optional) XSLT with Class XslCompiledTransform 19.11 Wrap-Up 19.12 Web Resources

6  2006 Pearson Education, Inc. All rights reserved. 6 19.1 Introduction Extensible Markup Language (XML) – Developed in 1996 by the W3C –.NET Framework uses XML extensively

7  2006 Pearson Education, Inc. All rights reserved. 7 19.2 XML Basics XML Document – Permits document’s authors to create markup for virtually any type of information – Contains text (data) and elements (the document’s structure) – Delimits elements with start tags ( ) and end tags ( ) – Must have exactly one root element that contains all the other elements

8  2006 Pearson Education, Inc. All rights reserved. 8 Outline Player.xml

9  2006 Pearson Education, Inc. All rights reserved. 9 19.2 XML Basics (Cont.) Viewing and Modifying XML Documents – Highly portable – Ends with.xml filename extension – Viewing or modifying an XML document does not require special software Any text editor that supports ASCII/Unicode characters can open XML documents for viewing and editing

10  2006 Pearson Education, Inc. All rights reserved. 10 19.2 XML Basics (Cont.) Processing XML Documents – Requires an XML parser Makes the document’s data available to applications – Can provide access to XML-encoded data in well-formed documents only Checks that the document follows the syntax rules specified by the W3C – Requires a single root element, a start tag and end tag for each element, and properly nested tags – XML is case sensitive The proper capitalization must be used in elements – A document that conforms to this syntax is a well-formed XML document Syntactically correct

11  2006 Pearson Education, Inc. All rights reserved. 11 19.2 XML Basics (Cont.) Validating XML Documents – An XML document can optionally reference a Document Type Definition (DTD) or a schema that defines the proper structure of the XML document – Parsers can read the DTD/schema and check that the XML document follows the structure defined by the DTD/schema XML documents that conform to the DTD/schema are valid – Nonvalidating parsers Parsers that cannot check for document conformity against DTDs/schemas Determine only whether an XML document is well formed

12  2006 Pearson Education, Inc. All rights reserved. 12 Software Engineering Observation 19.1 DTDs and schemas are essential for business-to-business (B2B) transactions and mission-critical systems. Validating XML documents ensures that disparate systems can manipulate data structured in standardized ways and prevents errors caused by missing or malformed data.

13  2006 Pearson Education, Inc. All rights reserved. 13 19.2 XML Basics (Cont.) Formatting and Manipulating XML Documents – XML documents contain only data Not formatting instructions – Applications that process XML documents must decide how to manipulate or display each document’s data – Extensible Stylesheet Language (XSL) Specifies the rendering instructions for different platforms – XML-processing programs can also search, sort and manipulate XML data using technologies such as XSL

14  2006 Pearson Education, Inc. All rights reserved. 14 19.3 Structuring Data XML Markup for an Article – XML comments Lines which begin with Can be placed almost anywhere in an XML document Can span to multiple lines – Blank lines, whitespaces and indentation are used in XML to improve readability Blank lines are normally ignored by XML parsers – XML element names can be of any length and may contain letters, digits, underscores, hyphens and periods Must begin with either a letter or an underscore Exception: Cannot begin with “xml” in any combination of uppercase and lowercase letters – This is reserved for use in the XML standards

15  2006 Pearson Education, Inc. All rights reserved. 15 Outline article.xml XML declaration; specifies the XML version to which the document conforms XML comments The root element Elements to mark up data

16  2006 Pearson Education, Inc. All rights reserved. 16 Portability Tip 19.1 Documents should include the XML declaration to identify the version of XML used. A document that lacks an XML declaration might be assumed to conform to the latest version of XML—when it does not, errors could result.

17  2006 Pearson Education, Inc. All rights reserved. 17 Common Programming Error 19.1 Placing whitespace characters before the XML declaration is an error.

18  2006 Pearson Education, Inc. All rights reserved. 18 Common Programming Error 19.2 In an XML document, each start tag must have a matching end tag; omitting either tag is an error. Soon, you will learn how such errors are detected.

19  2006 Pearson Education, Inc. All rights reserved. 19 Common Programming Error 19.3 XML is case sensitive. Using different cases for the start tag and end tag names for the same element is a syntax error.

20  2006 Pearson Education, Inc. All rights reserved. 20 Common Programming Error 19.4 Using a whitespace character in an XML element name is an error.

21  2006 Pearson Education, Inc. All rights reserved. 21 Good Programming Practice 19.1 XML element names should be meaningful to humans and should not use abbreviations.

22  2006 Pearson Education, Inc. All rights reserved. 22 Common Programming Error 19.5 Nesting XML tags improperly is a syntax error. For example, hello is an error, because the tag must precede the tag.

23  2006 Pearson Education, Inc. All rights reserved. 23 19.3 Structuring Data (Cont.) Viewing an XML Document in Internet Explorer – Internet Explorer uses a built-in style sheet to format the data – A “–” sign indicates that all child elements are being displayed Clicking the “-” sign collapses the container element and hides all the children – Clicking the “+” sign expands the container element and shows all the children

24  2006 Pearson Education, Inc. All rights reserved. 24 Fig. 19.3 | article.xml displayed by Internet Explorer. Information Bar Minus sign Expanded author element Plus sign Collapsed author element

25  2006 Pearson Education, Inc. All rights reserved. 25 19.3 Structuring Data (Cont.) XML Markup for a Business Letter – The DTD reference contains three items The name of the root element that the DTD specifies The keyword SYSTEM The DTD’s name and location – DTD document filenames typically end with the.dtd extension

26  2006 Pearson Education, Inc. All rights reserved. 26 Outline letter.xml (1 of 2) Specifies that this XML document references the “letter” DTD Indicates that this contact element identifies the letter’s sender Declare an empty element that contains data in attributes to specify the sender’s gender Indicates that this contact element identifies the letter’s recipient Declare an empty element because there is no further address information An empty element that contains data in attributes to specify the recipient's gender

27  2006 Pearson Education, Inc. All rights reserved. 27 Outline letter.xml (2 of 2)

28  2006 Pearson Education, Inc. All rights reserved. 28 Error-Prevention Tip 19.1 An XML document is not required to reference a DTD, but validating XML parsers can use a DTD to ensure that the document has the proper structure.

29  2006 Pearson Education, Inc. All rights reserved. 29 Portability Tip 19.2 Validating an XML document helps guarantee that independent developers will exchange data in a standardized form that conforms to the DTD.

30  2006 Pearson Education, Inc. All rights reserved. 30 Fig. 19.5 | Validating an XML document with Microsoft’s XML Validator.

31  2006 Pearson Education, Inc. All rights reserved. 31 Fig. 19.6 | Validation result using Microsoft’s XML Validator.

32  2006 Pearson Education, Inc. All rights reserved. 32 Common Programming Error 19.6 Failure to enclose attribute values in double ( "" ) or single ( '' ) quotes is a syntax error.

33  2006 Pearson Education, Inc. All rights reserved. 33 19.4 XML Namespaces XML allows document authors to create custom elements – Can result in naming collisions among elements XML namespace – Collection of element and attribute names – Place a namespace prefix and colon (:) before an element name to specify the namespace that the element belongs – Document authors can create their own namespace prefixes using virtually any name Except the reserved namespace prefix “xml”

34  2006 Pearson Education, Inc. All rights reserved. 34 Common Programming Error 19.7 Attempting to create a namespace prefix named xml in any mixture of uppercase and lowercase letters is a syntax error—the xml namespace prefix is reserved for internal use by XML itself.

35  2006 Pearson Education, Inc. All rights reserved. 35 19.4 XML Namespaces (Cont.) Differentiating Elements with Namespaces – Each namespace prefix is bound to a Uniform Resource Identifier (URI) Uniquely identifies the namespace Document authors create their own namespace prefixes and URIs A URI is a way to identifying a resource – Document authors must provide unique URIs To ensure that namespaces are unique – URIs employ the URN scheme frequently used to identify namespaces Under naming scheme: – A URI begins with “urn:” Followed by a unique series of additional names separated by colons

36  2006 Pearson Education, Inc. All rights reserved. 36 Outline namespace.xml Create two namespace prefixes Apply namespace prefix to elements

37  2006 Pearson Education, Inc. All rights reserved. 37 Outline defaultnamespace.xml Defines a default namespace using attribute xmlns with a URI Element file is in the default namespace Use a namespace prefix to specify a different namespace for another element

38  2006 Pearson Education, Inc. All rights reserved. 38 19.4 XML Namespaces (Cont.) Specifying a Default Namespace – Eliminate the need to place namespace prefixes in each element using the directory element

39  2006 Pearson Education, Inc. All rights reserved. 39 19.5 Document Type Definition (DTDs) Keywords – #IMPLIED Specifies that the parser can choose an arbitrary value for an attribute or can ignore an attribute if it finds a contact element without a type attribute – #REQUIRED Specifies that the attribute must be present in the element – #FIXED Specifies that the attribute must have the given fixed value – CDATA Specifies that attribute type contains character data A parser will pass such data to an application without modification – #PCDATA Specifies that an element may contain parsed character data – EMPTY Specifies that the element does not contain any data between its start and end tags

40  2006 Pearson Education, Inc. All rights reserved. 40 Software Engineering Observation 19.2 XML documents can have many different structures, and for this reason an application cannot be certain whether a particular document it receives is complete, ordered properly, and not missing data. DTDs and schemas (Section 19.6) solve this problem by providing an extensible way to describe XML document structure. Applications should use DTDs or schemas to confirm whether XML documents are valid.

41  2006 Pearson Education, Inc. All rights reserved. 41 Software Engineering Observation 19.3 Many organizations and individuals are creating DTDs and schemas for a broad range of applications. These collections—called repositories—are available free for download from the Web (e.g., www.xml.org, www.oasis-open.org ).

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline letter.dtd Defines the rules for element letter Defines attribute type The parser can ignore or choose an arbitrary value for the attribute Specifies that an element may contain parsed character data Defines an empty element named flag Specifies that gender attribute’s value must be M or F ; Default value is M

43  2006 Pearson Education, Inc. All rights reserved. 43 Common Programming Error 19.8 For documents validated with DTDs, any document that uses elements, attributes or relationships not explicitly defined by a DTD is an invalid document.

44  2006 Pearson Education, Inc. All rights reserved. 44 Software Engineering Observation 19.4 DTD syntax does not provide a mechanism for describing an element’s (or attribute’s) data type. For example, a DTD cannot specify that a particular element or attribute can contain only integer data.

45  2006 Pearson Education, Inc. All rights reserved. 45 Common Programming Error 19.9 Using markup characters (e.g., and & ) in parsed character data is an error. Use character entity references (e.g., <, > and & instead).

46  2006 Pearson Education, Inc. All rights reserved. 46 Fig. 19.10 | XML Validator displaying an error message.

47  2006 Pearson Education, Inc. All rights reserved. 47 19.6 W3C XML Schema Documents DTDs – Lack a way of indicating what specific type of data an element can contain – Are not themselves XML documents – Limitations have led to the development of schemas Schemas – Unlike DTDs, schemas do not use EBNF grammar – Like DTDs, schemas are used by validating parsers to validate documents – Use XML syntax – XML documents that programs can manipulate

48  2006 Pearson Education, Inc. All rights reserved. 48 19.6 W3C XML Schema Documents (Cont.) Validating Against an XML Schema Document – By convention, schemas use the.xsd extension Creating an XML Schema Document – The schema defines the elements, attributes and parent- child relationships that can included Also specifies the type of data that these elements and attributes may contain – The targetNamespace of the Schema must be the same as the namespace referenced of the XML document This is what “connects” the XML document with the schema that defines its structure

49  2006 Pearson Education, Inc. All rights reserved. 49 Outline book.xml Indicates that the books element is a part of the http://www.deitel.com/booklist namespace http://www.deitel.com/booklist

50  2006 Pearson Education, Inc. All rights reserved. 50 Outline book.xsd (1 of 2) Specifies the default namespace Binds the URI to namespace prefix deitel Specifies the link as the targetNamespace of the schema element specifies the actual elements that can be used to mark up data Define BooksType as a complex type that has a child element named book Indicates that a BooksType element can contain child elements named book of type deitel:SingleBookType

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline book.xsd (2 of 2) Define the complex type SingleBookType

52  2006 Pearson Education, Inc. All rights reserved. 52 Portability Tip 19.3 W3C XML Schema authors specify URI http://www.w3.org/2001/XMLSchema when referring to the XML Schema namespace. This namespace contains predefined elements that comprise the XML Schema vocabulary. Specifying this URI ensures that validation tools correctly identify XML Schema elements and do not confuse them with those defined by document authors.

53  2006 Pearson Education, Inc. All rights reserved. 53 19.6 W3C XML Schema Documents (Cont.) Defining an Element in XML Schema – The element tag defines an element that must be included in an XML document that conforms to it – Attributes name and type specifies the element’s name and data type An element’s data type indicates the data that the element may contain – Two categories of data type exist in XML Schema Simple and complex types differ only in that simple types cannot contain attributes or child elements and complex types can A user-defined type that contains attributes or child elements must be defined as a complex type

54  2006 Pearson Education, Inc. All rights reserved. 54 Fig. 19.13 | Some XML Schema data types. (Part 1 of 2.)

55  2006 Pearson Education, Inc. All rights reserved. 55 Fig. 19.13 | Some XML Schema data types. (Part 2 of 2.)

56  2006 Pearson Education, Inc. All rights reserved. 56 19.6 W3C XML Schema Documents (Cont.) A Closer Look at Types in XML Schema – Every element in XML Schema has a type – Every simple type defines a restriction on an XML Schema- defined type or a restriction on a user-defined type Restrictions limit the possible values that an element can hold – 2 complex types: (simple content or complex content) Both can contain attributes Simple content must extend or restrict some other existing type Only complex content can contain child elements – A document that conforms to a schema is known as an XML instance document The document is an instance of the schema

57  2006 Pearson Education, Inc. All rights reserved. 57 Outline computer.xsd (1 of 2) Create a simple type to describe the clock speed of the processor in gigahertz Declares base type as decimal Restrict the value to be at least 2.1 Declare a complex type with simple content Gives the complexType an attribute of type string named model

58  2006 Pearson Education, Inc. All rights reserved. 58 Outline computer.xsd (2 of 2) Encloses elements that must each be included once in the corresponding XML instance document Indicates that elements of type portable contain attribute of type string named manufacturer

59  2006 Pearson Education, Inc. All rights reserved. 59 Outline laptop.xml

60  2006 Pearson Education, Inc. All rights reserved. 60 19.7 (Optional) Extensible Stylesheet Language and XSL Transformations Extensible Stylesheet Language (XSL) – Specifies how programs are to render the XML data – XSL is a group of three technologies XSL-FO (XSL Formatting Objects) – Vocabulary for specifying formatting XPath (XML Path Language) – String-based language of expressions used by XML XSLT (XSL Transformations) – Technology for transforming XML documents into other documents Can convert XML into any text-based document – Have the extension.xsl – Source tree XML document to be transformed Must be properly structured – Result tree XML document to be created – Locates parts of the source tree document that match templates defined in an XSL style sheet Then the matching template executes and adds its result to the result tree - When no more matches, XSLT has transformed the source tree into the result tree

61  2006 Pearson Education, Inc. All rights reserved. 61 Outline sports.xml (1 of 2) Is a processing instruction that references the XSL style sheet sports.xsl

62  2006 Pearson Education, Inc. All rights reserved. 62 Outline sports.xml (2 of 2)

63  2006 Pearson Education, Inc. All rights reserved. 63 Software Engineering Observation 19.5 XSL enables document authors to separate data presentation (specified in XSL documents) from data description (specified in XML documents).

64  2006 Pearson Education, Inc. All rights reserved. 64 Outline sports.xsl (1 of 2) Specifies the XSLT version to which this document conforms and binds namespace prefix xsl Uses xsl:output to write an XHTML document type declaration to the result tree Select the document root of the XML source document Write the XHTML to result tree exactly as it appears in the XSL document

65  2006 Pearson Education, Inc. All rights reserved. 65 Outline sports.xsl (2 of 2) Iterate through the XML document, searching for game elements Places results in the result tree

66  2006 Pearson Education, Inc. All rights reserved. 66 Outline sorting.xml The XSL style sheet referenced can sort the XML file’s data

67  2006 Pearson Education, Inc. All rights reserved. 67 Outline sorting.xsl (1 of 4) Matches the root element of the document in Fig. 19.18 Outputs an html start tag to the result tree Specifies that the XSLT processor is to apply the xsl:template s Create the title for the XHTML document

68  2006 Pearson Education, Inc. All rights reserved. 68 Outline sorting.xsl (2 of 4) Create a header element that contains the book’s title Select each element that is a child of element frontMatter Retrieve the current node’s element name Retrieve the value of the pages attribute of the current node Sort chapters by number in ascending order

69  2006 Pearson Education, Inc. All rights reserved. 69 Outline sorting.xsl (3 of 4) Obtain the text between the chapter start and end tags Retrieve the value of the pages attribute of the current node

70  2006 Pearson Education, Inc. All rights reserved. 70 Outline sorting.xsl (4 of 4) Use an XSL variable to store the value of the book’s total page count and output the page count to the result tree

71  2006 Pearson Education, Inc. All rights reserved. 71 Fig. 19.20 | XSL style sheet elements. (Part 1 of 2.)

72  2006 Pearson Education, Inc. All rights reserved. 72 Fig. 19.20 | XSL style sheet elements. (Part 2 of 2.)

73  2006 Pearson Education, Inc. All rights reserved. 73 19.8 (Optional) Document Object Model (DOM) Some XML parsers store document data as tree structures in memory – This hierarchical tree structure is called a Document Object Model (DOM) tree – XML parser that creates this type of structure is known as a DOM parser – Each element name is represented by a node

74  2006 Pearson Education, Inc. All rights reserved. 74 Fig. 19.21 | Tree structure for the document article.xml of Fig. 19.2.

75  2006 Pearson Education, Inc. All rights reserved. 75 19.8 (Optional) Document Object Model (DOM) (Cont.) Abstract Class XmlReader – Iterate through each node in the XML document – Cannot create an object directly – static method Create Obtain an XmlReader reference – XmlReaderSettings object Specifies how the XmlReader to behave – Method Read Reads one node from the DOM tree at a time – NodeType property Specifies whether the node is an element, comment, text, XML declaration or end element – Indicated by using XmlNodeType enumeration constants

76  2006 Pearson Education, Inc. All rights reserved. 76 Outline XmlReaderTest.cs (1 of 4) using declaration for System.Xml namespace, which contains classes for Xml Create an object of XmlReaderSettings to specify how the XmlReader will behave Create a new object of XmlReader using method Create

77  2006 Pearson Education, Inc. All rights reserved. 77 Outline XmlReaderTest.cs (2 of 4) Reads one node at a time from the DOM tree Processes each of the read node Processes XML elements Output the elements’ names Processes XML comments Output the comments Processes XML texts Output the texts

78  2006 Pearson Education, Inc. All rights reserved. 78 Outline XmlReaderTest.cs (3 of 4) Processes XML declarations Output the declarations Processes XML end elements Output the end elements’ name

79  2006 Pearson Education, Inc. All rights reserved. 79 Outline XmlReaderTest.cs (4 of 4)

80  2006 Pearson Education, Inc. All rights reserved. 80 19.8 (Optional) Document Object Model (DOM) (Cont.) Displaying a DOM Tree Graphically in a TreeView Control – XmlReaders do not provide features for displaying their content graphically Display an XML document’s contents using a TreeView control – Use class TreeNode to represent each node in the tree

81  2006 Pearson Education, Inc. All rights reserved. 81 Outline XmlDom.cs (1 of 4) Represent each node in the tree The insignificant whitespaces in the XML document are ignored

82  2006 Pearson Education, Inc. All rights reserved. 82 Outline XmlDom.cs (2 of 4) Assign the XML document’s name to tree’s Text property Add the new TreeNode to the TreeView ’s Nodes collection Update the TreeView Declare newNode which will be added to the TreeView Add newNode to the TreeView Retrieve the node’s current parent

83  2006 Pearson Education, Inc. All rights reserved. 83 Outline XmlDom.cs (3 of 4) Add newNode to the TreeView

84  2006 Pearson Education, Inc. All rights reserved. 84 Outline XmlDom.cs (4 of 4) Updates the display to show the newly added TreeNode s Show all TreeNode s

85  2006 Pearson Education, Inc. All rights reserved. 85 19.8 (Optional) Document Object Model (DOM) (Cont.) Locating Data in XML Documents with XPath – XPathNavigator in the System.Xml.XPath namespace For iterating through node lists that match search criteria – Written as XPath expressions Method CreateNavigator – Creates and returns an XPathNavigator reference to the XPathDocument ’s tree structure The navigation methods of XPathNavigator : – MoveToFirstChild – MoveToParent – MoveToNext – MoveToPrevious – Each returns a boolean indicating whether the move was successful

86  2006 Pearson Education, Inc. All rights reserved. 86 Outline letter.xml (1 of 2)

87  2006 Pearson Education, Inc. All rights reserved. 87 Outline letter.xml (2 of 2)

88  2006 Pearson Education, Inc. All rights reserved. 88 Outline PathNavigator.cs (1 of 9) using declaration for System.Xml.XPath namespace for class XPathNavigator Create a XPathNavigator object Load sports.xml into document Create a XPathDocument object Creates and returns an navigator for sports.xml

89  2006 Pearson Education, Inc. All rights reserved. 89 Outline PathNavigator.cs (2 of 9) Create a XPathNodeIterator object Find and return any node that matches selectComboBox.Text

90  2006 Pearson Education, Inc. All rights reserved. 90 Outline PathNavigator.cs (3 of 9) Moves to the first child of the node referenced by the XPathNavigator

91  2006 Pearson Education, Inc. All rights reserved. 91 Outline PathNavigator.cs (4 of 9) Moves to the parent node of the node referenced by the XPathNavigator Determines the number many child nodes Removed the counted child nodes

92  2006 Pearson Education, Inc. All rights reserved. 92 Outline PathNavigator.cs (5 of 9) Moves to the next sibling of the node referenced by the XPathNavigator

93  2006 Pearson Education, Inc. All rights reserved. 93 Outline PathNavigator.cs (6 of 9) Moves to the previous sibling of the node referenced by the XPathNavigator

94  2006 Pearson Education, Inc. All rights reserved. 94 Outline PathNavigator.cs (7 of 9) Remove whitespaces

95  2006 Pearson Education, Inc. All rights reserved. 95 Outline PathNavigator.cs (8 of 9) (a) (b)

96  2006 Pearson Education, Inc. All rights reserved. 96 Outline PathNavigator.cs (9 of 9) (c) (d)

97  2006 Pearson Education, Inc. All rights reserved. 97 Outline Sports.xml

98  2006 Pearson Education, Inc. All rights reserved. 98 Fig. 19.27 | XPath expressions and descriptions.

99  2006 Pearson Education, Inc. All rights reserved. 99 19.9 (Optional) Schema Validation with Class XmlReader Validating an XML Document Programmatically – Class XmlReader Validate an XML document as it reads and parses the document – Validation is performed node-by-node by calling method Read – Class XmlSchemaSet Stores a collection of schemas against which an XmlReader can validate Method Add – Receives as arguments a namespace URI that identifies the schema and the name and location of the schema file – XmlReaderSettings ’s ValidationType property Indicates if we want the XmlReader to perform validation with a schema as it reads an XML document – XmlReaderSettings ’s Schemas property Used to validate the document read by the XmlReader

100  2006 Pearson Education, Inc. All rights reserved. 100 Outline ValidationTest.cs (1 of 3) using declaration for System.Xml.Schema namespace for class XmlSchemaSet Create a XmlSchemaSet object

101  2006 Pearson Education, Inc. All rights reserved. 101 Outline ValidationTest.cs (2 of 3) Add schema to the collection Create and set the properties of an XmlReaderSettings object Creates an XmlReader object that reads a file selected by the user and validates it against book.xsd schema Each call to Read validates the next node in the document Loop terminates when all nodes have been validated or when a node fails validation

102  2006 Pearson Education, Inc. All rights reserved. 102 Outline ValidationTest.cs (3 of 3)

103  2006 Pearson Education, Inc. All rights reserved. 103 Outline fail.xml (1 of 2) Fails validation, more than one title

104  2006 Pearson Education, Inc. All rights reserved. 104 Outline fail.xml (2 of 2)

105  2006 Pearson Education, Inc. All rights reserved. 105 19.10 (Optional) XSLT with Class XslCompiledTransform Class XslCompiledTransform – From the System.Xml.Xsl namespace – For applying XSLT style sheets to XML documents – Method Load Loads and compiles a style sheet – Method Transform Applies the compiled style sheet to a specified XML document

106  2006 Pearson Education, Inc. All rights reserved. 106 Outline TransformTest.cs (1 of 3) using declaration for System.Xml.Xsl namespace for class XslCompiledTransform Declares XslCompiledTransform reference transformer Instantiate transformer Parses and loads the style sheet

107  2006 Pearson Education, Inc. All rights reserved. 107 Outline TransformTest.cs (2 of 3) (a) (b) Transform sports.xml to XHTML and writes the result to disk as the file sports.html Reads all lines of a file into a string

108  2006 Pearson Education, Inc. All rights reserved. 108 Outline TransformTest.cs (3 of 3) (c)


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 19 Extensible Markup Language (XML)"

Similar presentations


Ads by Google