108
1 2006 Pearson Education, Inc. All rights rese 1 9 Extensible Markup Language (XML)

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

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

1

2006 Pearson Education, Inc. All rights reserved.

1919Extensible Markup Language (XML)

2

2006 Pearson Education, Inc. All rights reserved.

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.

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.

OBJECTIVESIn 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.

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.

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.

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 (<example>) and end tags (</example>)

– Must have exactly one root element that contains all the other elements

8

2006 Pearson Education, Inc. All rights reserved.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.1: player.xml -->

3 <!-- Baseball player structured with XML -->

4

5 <player>

6 <firstName>John</firstName>

7

8 <lastName>Doe</lastName>

9

10 <battingAverage>0.375</battingAverage>

11 </player>

Outline

Player.xml

9

2006 Pearson Education, Inc. All rights reserved.

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.

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.

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.

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.

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.

19.3 Structuring Data

• XML Markup for an Article– XML comments

• Lines which begin with <!-- and end 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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.2: article.xml -->

3 <!-- Article structured with XML -->

4

5 <article>

6 <title>Simple XML</title>

7

8 <date>May 5, 2005</date>

9

10 <author>

11 <firstName>John</firstName>

12 <lastName>Doe</lastName>

13 </author>

14

15 <summary>XML is pretty easy.</summary>

16

17 <content>

18 In this chapter, we present a wide variety of examples that use XML.

19 </content>

20 </article>

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.

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.

Common Programming Error 19.1

Placing whitespace characters before the XML declaration is an error.

18

2006 Pearson Education, Inc. All rights reserved.

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.

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.

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.

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.

Common Programming Error 19.5

Nesting XML tags improperly is a syntax error. For example, <x><y>hello</x></y> is an error, because the </y> tag must precede the </x> tag.

23

2006 Pearson Education, Inc. All rights reserved.

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.

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.

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.4: letter.xml -->

3 <!-- Business letter marked up as XML -->

4

5 <!DOCTYPE letter SYSTEM "letter.dtd">

6

7 <letter>

8 <contact type = "sender">

9 <name>Jane Doe</name>

10 <address1>Box 12345</address1>

11 <address2>15 Any Ave.</address2>

12 <city>Othertown</city>

13 <state>Otherstate</state>

14 <zip>67890</zip>

15 <phone>555-4321</phone>

16 <flag gender = "F" />

17 </contact>

18

19 <contact type = "receiver">

20 <name>John Doe</name>

21 <address1>123 Main St.</address1>

22 <address2></address2>

23 <city>Anytown</city>

24 <state>Anystate</state>

25 <zip>12345</zip>

26 <phone>555-1234</phone>

27 <flag gender = "M" />

28 </contact>

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.

29

30 <salutation>Dear Sir:</salutation>

31

32 <paragraph>It is our privilege to inform you about our new database

33 managed with XML. This new system allows you to reduce the

34 load on your inventory list server by having the client machine

35 perform the work of sorting and filtering the data.

36 </paragraph>

37

38 <paragraph>Please visit our Web site for availability

39 and pricing.

40 </paragraph>

41

42 <closing>Sincerely,</closing>

43 <signature>Ms. Jane Doe</signature>

44 </letter>

Outline

letter.xml

(2 of 2)

28

2006 Pearson Education, Inc. All rights reserved.

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.

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.

Fig. 19.5 | Validating an XML document with Microsoft’s XML Validator.

31

2006 Pearson Education, Inc. All rights reserved.

Fig. 19.6 | Validation result using Microsoft’s XML Validator.

32

2006 Pearson Education, Inc. All rights reserved.

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.

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.

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.

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.7: namespace.xml -->

3 <!-- Demonstrating namespaces -->

4

5 <text:directory

6 xmlns:text = "urn:deitel:textInfo"

7 xmlns:image = "urn:deitel:imageInfo">

8

9 <text:file filename = "book.xml">

10 <text:description>A book list</text:description>

11 </text:file>

12

13 <image:file filename = "funny.jpg">

14 <image:description>A funny picture</image:description>

15 <image:size width = "200" height = "100" />

16 </image:file>

17 </text:directory>

Outline

namespace.xml

Create two namespace prefixes

Apply namespace prefix to elements

37

2006 Pearson Education, Inc. All rights reserved.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.8: defaultnamespace.xml -->

3 <!-- Using default namespaces -->

4

5 <directory xmlns = "urn:deitel:textInfo"

6 xmlns:image = "urn:deitel:imageInfo">

7

8 <file filename = "book.xml">

9 <description>A book list</description>

10 </file>

11

12 <image:file filename = "funny.jpg">

13 <image:description>A funny picture</image:description>

14 <image:size width = "200" height = "100" />

15 </image:file>

16 </directory>

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.

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.

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.

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.

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.

1 <!-- Fig. 19.9: letter.dtd -->

2 <!-- DTD document for letter.xml -->

3

4 <!ELEMENT letter ( contact+, salutation, paragraph+,

5 closing, signature )>

6

7 <!ELEMENT contact ( name, address1, address2, city, state,

8 zip, phone, flag )>

9 <!ATTLIST contact type CDATA #IMPLIED>

10

11 <!ELEMENT name ( #PCDATA )>

12 <!ELEMENT address1 ( #PCDATA )>

13 <!ELEMENT address2 ( #PCDATA )>

14 <!ELEMENT city ( #PCDATA )>

15 <!ELEMENT state ( #PCDATA )>

16 <!ELEMENT zip ( #PCDATA )>

17 <!ELEMENT phone ( #PCDATA )>

18 <!ELEMENT flag EMPTY>

19 <!ATTLIST flag gender (M | F) "M">

20

21 <!ELEMENT salutation ( #PCDATA )>

22 <!ELEMENT closing ( #PCDATA )>

23 <!ELEMENT paragraph ( #PCDATA )>

24 <!ELEMENT signature ( #PCDATA )>

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.

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.

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.

Common Programming Error 19.9

Using markup characters (e.g., <, > and &) in parsed character data is an error. Use character entity references (e.g., &lt;, &gt; and &amp; instead).

46

2006 Pearson Education, Inc. All rights reserved.

Fig. 19.10 | XML Validator displaying an error message.

47

2006 Pearson Education, Inc. All rights reserved.

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.

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.11: book.xml -->

3 <!-- Book list marked up as XML -->

4

5 <deitel:books xmlns:deitel = "http://www.deitel.com/booklist">

6 <book>

7 <title>Visual Basic 2005 How to Program, 3/e</title>

8 </book>

9

10 <book>

11 <title>Visual C# 2005 How to Program</title>

12 </book>

13

14 <book>

15 <title>Java How to Program, 6/e</title>

16 </book>

17

18 <book>

19 <title>C++ How to Program, 5/e</title>

20 </book>

21

22 <book>

23 <title>Internet and World Wide Web How to Program, 3/e</title>

24 </book>

25 </deitel:books>

Outline

book.xml

Indicates that the books element is a part of the http://www.deitel.com/booklist namespace

50

2006 Pearson Education, Inc. All rights reserved.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.12: book.xsd -->

3 <!-- Simple W3C XML Schema document -->

4

5 <schema xmlns = "http://www.w3.org/2001/XMLSchema"

6 xmlns:deitel = "http://www.deitel.com/booklist"

7 targetNamespace = "http://www.deitel.com/booklist">

8

9 <element name = "books" type = "deitel:BooksType"/>

10

11 <complexType name = "BooksType">

12 <sequence>

13 <element name = "book" type = "deitel:SingleBookType"

14 minOccurs = "1" maxOccurs = "unbounded"/>

15 </sequence>

16 </complexType>

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.

17

18 <complexType name = "SingleBookType">

19 <sequence>

20 <element name = "title" type = "string"/>

21 </sequence>

22 </complexType>

23 </schema>

Outline

book.xsd

(2 of 2)

Define the complex type SingleBookType

52

2006 Pearson Education, Inc. All rights reserved.

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.

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.

Fig. 19.13 | Some XML Schema data types. (Part 1 of 2.)

XML Schema Data Type(s) Description Ranges or Structures Examples

String A character string. "hello"

Boolean True or false. true, false true

Decimal A decimal numeral. i * (10n), where i is an integer and n is an integer that is less than or equal to zero.

5, -12, -45.78

Float A floating-point number. m * (2e), where m is an integer whose absolute value is less than 224 and e is an integer in the range -149 to 104. Plus three additional numbers: positive infinity, negative infinity and not-a-number (NaN).

0, 12, -109.375, NaN

Double A floating-point number. m * (2e), where m is an integer whose absolute value is less than 253 and e is an integer in the range -1075 to 970. Plus three additional numbers: positive infinity, negative infinity and not-a-number (NaN).

0, 12, -109.375, NaN

55

2006 Pearson Education, Inc. All rights reserved.

Fig. 19.13 | Some XML Schema data types. (Part 2 of 2.)

XML Schema Data Type(s) Description Ranges or Structures Examples

Long A whole number. -9223372036854775808 to 9223372036854775807, inclusive

1234567890, -1234567890

Int A whole number. -2147483648 to 2147483647, inclusive

1234567890, -1234567890

Short A whole number. -32768 to 32767, inclusive 12, -345

date A date consisting of a year, month and day.

yyyy-mm with an optional dd and an optional time zone, where yyyy is four digits long and mm and dd are two digits long.

2005-05-10

time A time consisting of hours, minutes and seconds.

hh:mm:ss with an optional time zone, where hh, mm and ss are two digits long.

16:30:25-05:00

56

2006 Pearson Education, Inc. All rights reserved.

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.14: computer.xsd -->

3 <!-- W3C XML Schema document -->

4

5 <schema xmlns = "http://www.w3.org/2001/XMLSchema"

6 xmlns:computer = "http://www.deitel.com/computer"

7 targetNamespace = "http://www.deitel.com/computer">

8

9 <simpleType name = "gigahertz">

10 <restriction base = "decimal">

11 <minInclusive value = "2.1"/>

12 </restriction>

13 </simpleType>

14

15 <complexType name = "CPU">

16 <simpleContent>

17 <extension base = "string">

18 <attribute name = "model" type = "string"/>

19 </extension>

20 </simpleContent>

21 </complexType>

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.

22

23 <complexType name = "portable">

24 <all>

25 <element name = "processor" type = "computer:CPU"/>

26 <element name = "monitor" type = "int"/>

27 <element name = "CPUSpeed" type = "computer:gigahertz"/>

28 <element name = "RAM" type = "int"/>

29 </all>

30 <attribute name = "manufacturer" type = "string"/>

31 </complexType>

32

33 <element name = "laptop" type = "computer:portable"/>

34 </schema>

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.15: laptop.xml -->

3 <!-- Laptop components marked up as XML -->

4

5 <computer:laptop xmlns:computer = "http://www.deitel.com/computer"

6 manufacturer = "IBM">

7

8 <processor model = "Centrino">Intel</processor>

9 <monitor>17</monitor>

10 <CPUSpeed>2.4</CPUSpeed>

11 <RAM>256</RAM>

12 </computer:laptop>

Outline

laptop.xml

60

2006 Pearson Education, Inc. All rights reserved.

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.

1 <?xml version = "1.0"?>

2 <?xml:stylesheet type = "text/xsl" href = "sports.xsl"?>

3

4 <!-- Fig. 19.16: sports.xml -->

5 <!-- Sports Database -->

6

7 <sports>

8 <game id = "783">

9 <name>Cricket</name>

10

11 <paragraph>

12 More popular among commonwealth nations.

13 </paragraph>

14 </game>

15

16 <game id = "239">

17 <name>Baseball</name>

18

19 <paragraph>

20 More popular in America.

21 </paragraph>

22 </game>

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.

23

24 <game id = "418">

25 <name>Soccer (Futbol)</name>

26

27 <paragraph>

28 Most popular sport in the world.

29 </paragraph>

30 </game>

31 </sports>

Outline

sports.xml

(2 of 2)

63

2006 Pearson Education, Inc. All rights reserved.

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.17: sports.xsl -->

3 <!-- A simple XSLT transformation -->

4

5 <!-- reference XSL style sheet URI -->

6 <xsl:stylesheet version = "1.0"

7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

8

9 <xsl:output method = "xml" omit-xml-declaration = "no"

10 doctype-system =

11 "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd"

12 doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"/>

13

14 <xsl:template match = "/"> <!-- match root element -->

15

16 <html xmlns = "http://www.w3.org/1999/xhtml">

17 <head>

18 <title>Sports</title>

19 </head>

20

21 <body>

22 <table border = "1" bgcolor = "wheat">

23 <thead>

24 <tr>

25 <th>ID</th>

26 <th>Sport</th>

27 <th>Information</th>

28 </tr>

29 </thead>

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.

30

31 <!-- insert each name and paragraph element value -->

32 <!-- into a table row. -->

33 <xsl:for-each select = "/sports/game">

34 <tr>

35 <td><xsl:value-of select = "@id"/></td>

36 <td><xsl:value-of select = "name"/></td>

37 <td><xsl:value-of select = "paragraph"/></td>

38 </tr>

39 </xsl:for-each>

40 </table>

41 </body>

42 </html>

43

44 </xsl:template>

45 </xsl:stylesheet>

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.18: sorting.xml -->

3 <!-- XML document containing book information -->

4

5 <?xml:stylesheet type = "text/xsl" href = "sorting.xsl"?>

6

7 <book isbn = "999-99999-9-X">

8 <title>Deitel&apos;s XML Primer</title>

9

10 <author>

11 <firstName>Jane</firstName>

12 <lastName>Blue</lastName>

13 </author>

14

15 <chapters>

16 <frontMatter>

17 <preface pages = "2" />

18 <contents pages = "5" />

19 <illustrations pages = "4" />

20 </frontMatter>

21

22 <chapter number = "3" pages = "44">Advanced XML</chapter>

23 <chapter number = "2" pages = "35">Intermediate XML</chapter>

24 <appendix number = "B" pages = "26">Parsers and Tools</appendix>

25 <appendix number = "A" pages = "7">Entities</appendix>

26 <chapter number = "1" pages = "28">XML Fundamentals</chapter>

27 </chapters>

28

29 <media type = "CD" />

30 </book>

Outline

sorting.xml

The XSL style sheet referenced can sort the XML file’s data

67

2006 Pearson Education, Inc. All rights reserved.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.19: sorting.xsl -->

3 <!-- Transformation of book information into XHTML -->

4

5 <xsl:stylesheet version = "1.0"

6 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

7

8 <!-- write XML declaration and DOCTYPE DTD information -->

9 <xsl:output method = "xml" omit-xml-declaration = "no"

10 doctype-system = "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"

11 doctype-public = "-//W3C//DTD XHTML 1.1//EN"/>

12

13 <!-- match document root -->

14 <xsl:template match = "/">

15 <html xmlns = "http://www.w3.org/1999/xhtml">

16 <xsl:apply-templates/>

17 </html>

18 </xsl:template>

19

20 <!-- match book -->

21 <xsl:template match = "book">

22 <head>

23 <title>ISBN <xsl:value-of select = "@isbn"/> -

24 <xsl:value-of select = "title"/></title>

25 </head>

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:templates

Create the title for the XHTML document

68

2006 Pearson Education, Inc. All rights reserved.

26

27 <body>

28 <h1 style = "color: blue"><xsl:value-of select = "title"/></h1>

29 <h2 style = "color: blue">by

30 <xsl:value-of select = "author/lastName"/>,

31 <xsl:value-of select = "author/firstName"/></h2>

32

33 <table style = "border-style: groove; background-color: wheat">

34

35 <xsl:for-each select = "chapters/frontMatter/*">

36 <tr>

37 <td style = "text-align: right">

38 <xsl:value-of select = "name()"/>

39 </td>

40

41 <td>

42 ( <xsl:value-of select = "@pages"/> pages )

43 </td>

44 </tr>

45 </xsl:for-each>

46

47 <xsl:for-each select = "chapters/chapter">

48 <xsl:sort select = "@number" data-type = "number"

49 order = "ascending"/>

50 <tr>

51 <td style = "text-align: right">

52 Chapter <xsl:value-of select = "@number"/>

53 </td>

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.

54

55 <td>

56 <xsl:value-of select = "text()"/>

57 ( <xsl:value-of select = "@pages"/> pages )

58 </td>

59 </tr>

60 </xsl:for-each>

61

62 <xsl:for-each select = "chapters/appendix">

63 <xsl:sort select = "@number" data-type = "text"

64 order = "ascending"/>

65 <tr>

66 <td style = "text-align: right">

67 Appendix <xsl:value-of select = "@number"/>

68 </td>

69

70 <td>

71 <xsl:value-of select = "text()"/>

72 ( <xsl:value-of select = "@pages"/> pages )

73 </td>

74 </tr>

75 </xsl:for-each>

76 </table>

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.

77

78 <br /><p style = "color: blue">Pages:

79 <xsl:variable name = "pagecount"

80 select = "sum(chapters//*/@pages)"/>

81 <xsl:value-of select = "$pagecount"/>

82 <br />Media Type: <xsl:value-of select = "media/@type"/></p>

83 </body>

84 </xsl:template>

85 </xsl:stylesheet>

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.

Fig. 19.20 | XSL style sheet elements. (Part 1 of 2.)

Element Description

<xsl:apply-templates> Applies the templates of the XSL document to the children of the current node.

<xsl:apply-templates match = "expression">

Applies the templates of the XSL document to the children of expression. The value of the attribute match (i.e., expression) must be an XPath expression that specifies elements.

<xsl:template> Contains rules to apply when a specified node is matched.

<xsl:value-of select = "expression">

Selects the value of an XML element and adds it to the output tree of the transformation. The required select attribute contains an XPath expression.

72

2006 Pearson Education, Inc. All rights reserved.

Element Description

<xsl:for-each select = "expression">

Applies a template to every node selected by the XPath specified by the select attribute.

<xsl:sort select = "expression">

Used as a child element of an <xsl:apply-templates> or <xsl:for-each> element. Sorts the nodes selected by the <xsl:apply-template> or <xsl:for-each> element so that the nodes are processed in sorted order.

<xsl:output> Has various attributes to define the format (e.g., XML, XHTML), version (e.g., 1.0, 2.0), document type and media type of the output document. This tag is a top-level element—it can be used only as a child element of an xml:stylesheet.

<xsl:copy> Adds the current node to the output tree.

Fig. 19.20 | XSL style sheet elements. (Part 2 of 2.)

73

2006 Pearson Education, Inc. All rights reserved.

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.

Fig. 19.21 | Tree structure for the document article.xml of Fig. 19.2.

75

2006 Pearson Education, Inc. All rights reserved.

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.

1 // Fig. 19.22: XmlReaderTest.cs

2 // Reading an XML document.

3 using System;

4 using System.Windows.Forms;

5 using System.Xml;

6

7 namespace XmlReaderTest

8 {

9 public partial class XmlReaderTestForm : Form

10 {

11 public XmlReaderTestForm()

12 {

13 InitializeComponent();

14 } // end constructor

15

16 // read XML document and display its content

17 private void XmlReaderTestForm_Load( object sender, EventArgs e )

18 {

19 // create the XmlReader object

20 XmlReaderSettings settings = new XmlReaderSettings();

21 XmlReader reader = XmlReader.Create( "article.xml", settings );

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.

22

23 int depth = -1; // tree depth is -1, no indentation

24

25 while ( reader.Read() ) // display each node's content

26 {

27 switch ( reader.NodeType )

28 {

29 case XmlNodeType.Element: // XML Element, display its name

30 depth++; // increase tab depth

31 TabOutput( depth ); // insert tabs

32 OutputTextBox.Text += "<" + reader.Name + ">\r\n";

33

34 // if empty element, decrease depth

35 if ( reader.IsEmptyElement )

36 depth--;

37 break;

38 case XmlNodeType.Comment: // XML Comment, display it

39 TabOutput( depth ); // insert tabs

40 OutputTextBox.Text += "<!--" + reader.Value + "-->\r\n";

41 break;

42 case XmlNodeType.Text: // XML Text, display it

43 TabOutput( depth ); // insert tabs

44 OutputTextBox.Text += "\t" + reader.Value + "\r\n";

45 break;

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.

46

47 // XML XMLDeclaration, display it

48 case XmlNodeType.XmlDeclaration:

49 TabOutput( depth ); // insert tabs

50 OutputTextBox.Text += "<?" + reader.Name + " " +

51 reader.Value + "?>\n";

52 break;

53 case XmlNodeType.EndElement: // XML EndElement, display it

54 TabOutput( depth ); // insert tabs

55 OutputTextBox.Text += "</" + reader.Name + ">\r\n";

56 depth--; // decrement depth

57 break;

58 } // end switch

59 } // end while

60 } // end method XmlReaderTextForm_Load

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.

61

62 // insert tabs

63 private void TabOutput( int number )

64 {

65 for ( int i = 0; i < number; i++ )

66 OutputTextBox.Text += "\t";

67 } // end method TabOutput

68 } // end class XmlReaderTestForm

69 } // end namespace XmlReaderTest

Outline

XmlReaderTest.cs

(4 of 4)

80

2006 Pearson Education, Inc. All rights reserved.

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.

1 // Fig. 19.23: XmlDom.cs

2 // Demonstrates DOM tree manipulation.

3 using System;

4 using System.Windows.Forms;

5 using System.Xml;

6

7 namespace XmlDom

8 {

9 public partial class XmlDomForm : Form

10 {

11 public XmlDomForm()

12 {

13 InitializeComponent();

14 } // end constructor

15

16 private TreeNode tree; // TreeNode reference

17

18 // initialize instance variables

19 private void XmlDomForm_Load( object sender, EventArgs e )

20 {

21 // create Xml ReaderSettings and

22 // set the IgnoreWhitespace property

23 XmlReaderSettings settings = new XmlReaderSettings();

24 settings.IgnoreWhitespace = true;

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.

25

26 // create XmlReader object

27 XmlReader reader = XmlReader.Create( "letter.xml", settings );

28

29 tree = new TreeNode(); // instantiate TreeNode

30

31 tree.Text = "letter.xml"; // assign name to TreeNode

32 xmlTreeView.Nodes.Add( tree ); // add TreeNode to TreeView

33 BuildTree( reader, tree ); // build node and tree hierarchy

34 } // end method XmlDomForm_Load

35

36 // construct TreeView based on DOM tree

37 private void BuildTree( XmlReader reader, TreeNode treeNode )

38 {

39 // treeNode to add to existing tree

40 TreeNode newNode = new TreeNode();

41

42 while ( reader.Read() )

43 {

44 // build tree based on node type

45 switch ( reader.NodeType )

46 {

47 // if Text node, add its value to tree

48 case XmlNodeType.Text:

49 newNode.Text = reader.Value;

50 treeNode.Nodes.Add( newNode );

51 break;

52 case XmlNodeType.EndElement: // if EndElement, move up tree

53 treeNode = treeNode.Parent;

54 break;

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.

55

56 // if new element, add name and traverse tree

57 case XmlNodeType.Element:

58

59 // determine if element contains content

60 if ( !reader.IsEmptyElement )

61 {

62 // assign node text, add newNode as child

63 newNode.Text = reader.Name;

64 treeNode.Nodes.Add( newNode );

65

66 // set treeNode to last child

67 treeNode = newNode;

68 } // end if

69 else // do not traverse empty elements

70 {

71 // assign NodeType string to newNode

72 // and add it to tree

73 newNode.Text = reader.NodeType.ToString();

74 treeNode.Nodes.Add( newNode );

75 } // end else

76 break;

77 default: // all other types, display node type

78 newNode.Text = reader.NodeType.ToString();

79 treeNode.Nodes.Add( newNode );

80 break;

81 } // end switch

Outline

XmlDom.cs

(3 of 4)

Add newNode to the TreeView

84

2006 Pearson Education, Inc. All rights reserved.

82

83 newNode = new TreeNode();

84 } // end while

85

86 // update TreeView control

87 xmlTreeView.ExpandAll(); // expand tree nodes in TreeView

88 xmlTreeView.Refresh(); // force TreeView to update

89 } // end method BuildTree

90 } // end class XmlDomForm

91 } // end namespace XmlDom

Outline

XmlDom.cs

(4 of 4) Updates the display to show the newly added TreeNodes

Show all TreeNodes

85

2006 Pearson Education, Inc. All rights reserved.

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.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.24: letter.xml -->

3 <!-- Business letter formatted with XML -->

4

5 <letter>

6 <contact type = "sender">

7 <name>Jane Doe</name>

8 <address1>Box 12345</address1>

9 <address2>15 Any Ave.</address2>

10 <city>Othertown</city>

11 <state>Otherstate</state>

12 <zip>67890</zip>

13 <phone>555-4321</phone>

14 <flag gender = "F" />

15 </contact>

16

17 <contact type = "receiver">

18 <name>John Doe</name>

19 <address1>123 Main St.</address1>

20 <address2></address2>

21 <city>Anytown</city>

22 <state>Anystate</state>

23 <zip>12345</zip>

24 <phone>555-1234</phone>

25 <flag gender = "M" />

26 </contact>

Outline

letter.xml

(1 of 2)

87

2006 Pearson Education, Inc. All rights reserved.

27

28 <salutation>Dear Sir:</salutation>

29

30 <paragraph>It is our privilege to inform you about our new database

31 managed with XML. This new system allows you to reduce the

32 load on your inventory list server by having the client machine

33 perform the work of sorting and filtering the data.

34 </paragraph>

35

36 <paragraph>Please visit our Web site for availability

37 and pricing.

38 </paragraph>

39

40 <closing>Sincerely,</closing>

41 <signature>Ms. Doe</signature>

42 </letter>

Outline

letter.xml

(2 of 2)

88

2006 Pearson Education, Inc. All rights reserved.

1 // Fig. 19.25: PathNavigator.cs

2 // Demonstrates class XPathNavigator.

3 using System;

4 using System.Windows.Forms;

5 using System.Xml.XPath; // contains XPathNavigator

6

7 namespace PathNavigator

8 {

9 public partial class PathNavigatorForm : Form

10 {

11 public PathNavigatorForm()

12 {

13 InitializeComponent();

14 } // end constructor

15

16 private XPathNavigator xPath; // navigator to traverse document

17

18 // references document for use by XPathNavigator

19 private XPathDocument document;

20

21 // references TreeNode list used by TreeView control

22 private TreeNode tree;

23

24 // initialize variables and TreeView control

25 private void PathNavigatorForm_Load( object sender, EventArgs e )

26 {

27 // load XML document

28 document = new XPathDocument( "sports.xml" );

29 xPath = document.CreateNavigator(); // create navigator

30 tree = new TreeNode(); // create root node for TreeNodes

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.

31

32 tree.Text = xPath.NodeType.ToString(); // #root

33 pathTreeView.Nodes.Add( tree ); // add tree

34

35 // update TreeView control

36 pathTreeView.ExpandAll(); // expand tree node in TreeView

37 pathTreeView.Refresh(); // force TreeView update

38 pathTreeView.SelectedNode = tree; // highlight root

39 } // end method PathNavigatorForm_Load

40

41 // process selectButton_Click event

42 private void selectButton_Click( object sender, EventArgs e )

43 {

44 XPathNodeIterator iterator; // enables node iteration

45

46 try // get specified node from ComboBox

47 {

48 // select specified node

49 iterator = xPath.Select( selectComboBox.Text );

50 DisplayIterator( iterator ); // print selection

51 } // end try

52 // catch invalid expressions

53 catch ( System.Xml.XPath.XPathException argumentException )

54 {

55 MessageBox.Show( argumentException.Message, "Error",

56 MessageBoxButtons.OK, MessageBoxIcon.Error );

57 } // end catch

58 } // end method selectButton_Click

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.

59

60 // traverse to first child on firstChildButton_Click event

61 private void firstChildButton_Click( object sender, EventArgs e )

62 {

63 TreeNode newTreeNode;

64

65 // move to first child

66 if ( xPath.MoveToFirstChild() )

67 {

68 newTreeNode = new TreeNode(); // create new node

69

70 // set node's Text property to

71 // either navigator's name or value

72 DetermineType( newTreeNode, xPath );

73

74 // add nodes to TreeNode node list

75 tree.Nodes.Add( newTreeNode );

76 tree = newTreeNode; // assign tree newTreeNode

77

78 // update TreeView control

79 pathTreeView.ExpandAll(); // expand node in TreeView

80 pathTreeView.Refresh(); // force TreeView to update

81 pathTreeView.SelectedNode = tree; // highlight root

82 } // end if

83 else // node has no children

84 MessageBox.Show( "Current Node has no children.",

85 "", MessageBoxButtons.OK, MessageBoxIcon.Information );

86 } // end method firstChildButton_Click

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.

87

88 // traverse to node's parent on parentButton_Click event

89 private void parentButton_Click( object sender, EventArgs e )

90 {

91 // move to parent

92 if ( xPath.MoveToParent() )

93 {

94 tree = tree.Parent;

95

96 // get number of child nodes, not including sub trees

97 int count = tree.GetNodeCount( false );

98

99 // remove all children

100 for ( int i = 0; i < count; i++ )

101 tree.Nodes.Remove( tree.FirstNode ); // remove child node

102

103 // update TreeView control

104 pathTreeView.ExpandAll(); // expand node in TreeView

105 pathTreeView.Refresh(); // force TreeView to update

106 pathTreeView.SelectedNode = tree; // highlight root

107 } // end if

108 else // if node has no parent (root node)

109 MessageBox.Show( "Current node has no parent.", "",

110 MessageBoxButtons.OK, MessageBoxIcon.Information );

111 } // end method parentButton_Click

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.

112

113 // find next sibling on nextButton_Click event

114 private void nextButton_Click( object sender, EventArgs e )

115 {

116 // declare and initialize two TreeNodes

117 TreeNode newTreeNode = null;

118 TreeNode newNode = null;

119

120 // move to next sibling

121 if ( xPath.MoveToNext() )

122 {

123 newTreeNode = tree.Parent; // get parent node

124

125 newNode = new TreeNode(); // create new node

126

127 // decide whether to display current node

128 DetermineType( newNode, xPath );

129 newTreeNode.Nodes.Add( newNode ); // add to parent node

130

131 tree = newNode; // set current position for display

132

133 // update TreeView control

134 pathTreeView.ExpandAll(); // expand node in Tree‘‘‘‘View

135 pathTreeView.Refresh(); // force TreeView to update

136 pathTreeView.SelectedNode = tree; // highlight root

137 } // end if

138 else // node has no additional siblings

139 MessageBox.Show( "Current node is last sibling.", "",

140 MessageBoxButtons.OK, MessageBoxIcon.Information );

141 } // end method nextButton_Click

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.

142

143 // get previous sibling on previousButton_Click

144 private void previousButton_Click( object sender, EventArgs e )

145 {

146 TreeNode parentTreeNode = null;

147

148 // move to previous sibling

149 if ( xPath.MoveToPrevious() )

150 {

151 parentTreeNode = tree.Parent; // get parent node

152 parentTreeNode.Nodes.Remove( tree ); // delete current node

153 tree = parentTreeNode.LastNode; // move to previous node

154

155 // update TreeView control

156 pathTreeView.ExpandAll(); // expand tree node in TreeView

157 pathTreeView.Refresh(); // force TreeView to update

158 pathTreeView.SelectedNode = tree; // highlight root

159 } // end if

160 else // if current node has no previous siblings

161 MessageBox.Show( "Current node is first sibling.", "",

162 MessageBoxButtons.OK, MessageBoxIcon.Information );

163 } // end method previousButton_Click

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.

164

165 // print values for XPathNodeIterator

166 private void DisplayIterator( XPathNodeIterator iterator )

167 {

168 selectTextBox.Clear();

169

170 // prints selected node's values

171 while ( iterator.MoveNext() )

172 selectTextBox.Text += iterator.Current.Value.Trim() + "\r\n";

173 } // end method DisplayIterator

174

175 // determine if TreeNode should display current node name or value

176 private void DetermineType( TreeNode node, XPathNavigator xPath )

177 {

178 switch ( xPath.NodeType ) // determine NodeType

179 {

180 case XPathNodeType.Element: // if Element, get its name

181 // get current node name, and remove whitespaces

182 node.Text = xPath.Name.Trim();

183 break;

184 default: // obtain node values

185 // get current node value and remove whitespaces

186 node.Text = xPath.Value.Trim();

187 break;

188 } // end switch

189 } // end method DetermineType

190 } // end class PathNavigatorForm

191 } // end namespace PathNavigator

Outline

PathNavigator.cs

(7 of 9)

Remove whitespaces

95

2006 Pearson Education, Inc. All rights reserved.

Outline

PathNavigator.cs

(8 of 9)

(a) (b)

96

2006 Pearson Education, Inc. All rights reserved.

Outline

PathNavigator.cs

(9 of 9)

(c) (d)

97

2006 Pearson Education, Inc. All rights reserved.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.26: sports.xml -->

3 <!-- Sports Database -->

4

5 <sports>

6 <game id = "783">

7 <name>Cricket</name>

8

9 <paragraph>

10 More popular among commonwealth nations.

11 </paragraph>

12 </game>

13

14 <game id = "239">

15 <name>Baseball</name>

16

17 <paragraph>

18 More popular in America.

19 </paragraph>

20 </game>

21

22 <game id = "418">

23 <name>Soccer (Futbol)</name>

24

25 <paragraph>

26 Most popular sport in the world.

27 </paragraph>

28 </game>

29 </sports>

Outline

Sports.xml

98

2006 Pearson Education, Inc. All rights reserved.

Fig. 19.27 | XPath expressions and descriptions.

XPath Expression Description

/sports Matches all sports nodes that are child nodes of the document root node.

/sports/game Matches all game nodes that are child nodes of sports, which is a child of the document root.

/sports/game/name

Matches all name nodes that are child nodes of game. The game is a child of sports, which is a child of the document root.

/sports/game/paragraph Matches all paragraph nodes that are child nodes of game. The game is a child of sports, which is a child of the document root.

/sports/game [name='Cricket'] Matches all game nodes that contain a child element whose name is Cricket. The game is a child of sports, which is a child of the document root.

99

2006 Pearson Education, Inc. All rights reserved.

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.

1 // Fig. 19.28: ValidationTest.cs

2 // Validating XML documents against schemas.

3 using System;

4 using System.Windows.Forms;

5 using System.Xml;

6 using System.Xml.Schema; // contains XmlSchemaSet class

7

8 namespace ValidationTest

9 {

10 public partial class ValidationTestForm : Form

11 {

12 public ValidationTestForm()

13 {

14 InitializeComponent();

15 } // end constructor

16

17 private XmlSchemaSet schemas; // schemas to validate against

18 private bool valid = true; // validation result

19

20 // handle validateButton Click event

21 private void validateButton_Click( object sender, EventArgs e )

22 {

23 schemas = new XmlSchemaSet(); // create the XmlSchemaSet class

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.

24

25 // add the schema to the collection

26 schemas.Add( "http://www.deitel.com/booklist", "book.xsd" );

27

28 // set the validation settings

29 XmlReaderSettings settings = new XmlReaderSettings();

30 settings.ValidationType = ValidationType.Schema;

31 settings.Schemas = schemas;

32 settings.ValidationEventHandler += ValidationError;

33

34 // create the XmlReader object

35 XmlReader reader =

36 XmlReader.Create( filesComboBox.Text, settings );

37

38 // parse the file

39 while ( reader.Read() ); // empty body

40

41 if ( valid ) // check validation result

42 {

43 consoleLabel.Text = "Document is valid";

44 } // end if

45

46 valid = true; // reset variable

47 reader.Close(); // close reader stream

48 } // end method validateButton_Click

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.

49

50 // event handler for validation error

51 private void ValidationError( object sender ,

52 ValidationEventArgs arguments )

53 {

54 lblConsole.Text = arguments.Message;

55 valid = false; // validation failed

56 } // end method ValidationError

57 } // end class ValidatorTestForm

58 } // end namespace ValidatorTest

Outline

ValidationTest.cs

(3 of 3)

103

2006 Pearson Education, Inc. All rights reserved.

1 <?xml version = "1.0"?>

2 <!-- Fig. 19.29: fail.xml -->

3 <!-- XML file that does not conform to schema book.xsd -->

4

5 <deitel:books xmlns:deitel = "http://www.deitel.com/booklist">

6 <book>

7 <title>Visual Basic 2005 How to Program, 3/e</title>

8 </book>

9

10 <book>

11 <title>Visual C# 2005 How to Program</title>

12 </book>

13

14 <book>

15 <title>Java How to Program, 6/e</title>

16 </book>

17

18 <book>

19 <title>C++ How to Program, 5/e</title>

20 <title>Internet and World Wide Web How to Program, 3/e</title>

21 </book>

22

23 <book>

24 <title>XML How to Program</title>

25 </book>

26 </deitel:books>

Outline

fail.xml

(1 of 2)

Fails validation, more than one title

104

2006 Pearson Education, Inc. All rights reserved.

Outline

fail.xml

(2 of 2)

105

2006 Pearson Education, Inc. All rights reserved.

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.

1 // Fig. 19.30: TransformTest.cs

2 // Applying an XSLT style sheet to an XML document.

3 using System;

4 using System.Windows.Forms;

5 using System.Xml.Xsl; // contains class XslCompiledTransform

6

7 namespace TransformTest

8 {

9 public partial class TransformTestForm : Form

10 {

11 public TransformTestForm()

12 {

13 InitializeComponent();

14 } // end constructor

15

16 // applies the transformation

17 private XslCompiledTransform transformer;

18

19 // initialize variables

20 private void TransformTestForm_Load( object sender, EventArgs e )

21 {

22 transformer = new XslCompiledTransform(); // create transformer

23

24 // load and compile the style sheet

25 transformer.Load( "sports.xsl" );

26 } // end method TransformTestForm_Load

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.

27

28 // transform XML data on transformButton_Click event

29 private void transformButton_Click( object sender, EventArgs e )

30 {

31 // perform the transformation and store the result in new file

32 transformer.Transform( "sports.xml", "sports.html" );

33

34 // read and display the XHTML document's text in a Textbox

35 consoleTextBox.Text =

36 System.IO.File.ReadAllText( "sports.html" );

37 } // end method transformButton_Click

38 } // end class TransformTestForm

39 } // end namespace TransformTest

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.

Outline

TransformTest.cs

(3 of 3)

(c)