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

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

What is XML? a meta language that allows you to create and format your own document markups a method for putting structured data into a text file; these.
XML: Extensible Markup Language
An Introduction to XML Based on the W3C XML Recommendations.
3 November 2008CIS 340 # 1 Topics To define XML as a technology To place XML in the context of system architectures.
XML Schemas Microsoft XML Schemas W3C XML Schemas.
 2002 Prentice Hall, Inc. All rights reserved. ISQA 407 XML/WML Winter 2002 Dr. Sergio Davalos.
Chapter 10 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 10 Sebesta: Programming the World Wide Web.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic XML.
Creating a Well-Formed Valid Document. 2 Objectives Introducing XHTML Creating a Well-Formed Document Creating a Valid Document Creating an XHTML Document.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
Tutorial 11 Creating XML Document
COS 381 Day 14. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
Document Type Definitions. XML and DTDs A DTD (Document Type Definition) describes the structure of one or more XML documents. Specifically, a DTD describes:
Introduction to XML This material is based heavily on the tutorial by the same name at
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
ECA 228 Internet/Intranet Design I Intro to XML. ECA 228 Internet/Intranet Design I HTML markup language very loose standards browsers adjust for non-standard.
Chapter 12 Creating and Using XML Documents HTML5 AND CSS Seventh Edition.
 2008 Pearson Education, Inc. All rights reserved XML.
XP New Perspectives on XML Tutorial 4 1 XML Schema Tutorial – Carey ISBN Working with Namespaces and Schemas.
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
XP Tutorial 9New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with XHTML Creating a Well-Formed Valid Document Tutorial 9.
XML Anisha K J Jerrin Thomas. Outline  Introduction  Structure of an XML Page  Well-formed & Valid XML Documents  DTD – Elements, Attributes, Entities.
Introduction to XML cs3505. References –I got most of this presentation from this site –O’reilly tutorials.
Why XML ? Problems with HTML HTML design - HTML is intended for presentation of information as Web pages. - HTML contains a fixed set of markup tags. This.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
 2008 Pearson Education, Inc. All rights reserved XML and RSS.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 20 – Extensible Markup Language (XML) Outline 20.1 Introduction 20.2 Structuring Data 20.3 XML.
 2003 Prentice Hall, Inc. All rights reserved. 3 rd Edition Slide 1 Chapter 20 – Extensible Markup Language (XML) Outline 20.1 Introduction 20.2 Structuring.
1 © Netskills Quality Internet Training, University of Newcastle Introducing XML © Netskills, Quality Internet Training University.
XML 1 Enterprise Applications CE00465-M XML. 2 Enterprise Applications CE00465-M XML Overview Extensible Mark-up Language (XML) is a meta-language that.
What is XML?  XML stands for EXtensible Markup Language  XML is a markup language much like HTML  XML was designed to carry data, not to display data.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
Tutorial 1: XML Creating an XML Document. 2 Introducing XML XML stands for Extensible Markup Language. A markup language specifies the structure and content.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
1 Tutorial 13 Validating Documents with DTDs Working with Document Type Definitions.
Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple values (child elements can) Attributes are not easily.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 6 – Document Type Definition (DTD) Outline 6.1Introduction 6.2Parsers, Well-formed and Valid XML.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
Softsmith Infotech XML. Softsmith Infotech XML EXtensible Markup Language XML is a markup language much like HTML Designed to carry data, not to display.
E0262 – MIS – Multimedia Storage Techniques XML (Extensible Markup Language  XML is a markup language for creating documents containing structured information.
XML 2nd EDITION Tutorial 1 Creating An Xml Document.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 20 – Extensible Markup Language (XML) Outline 20.1 Introduction 20.2 Structuring Data 20.3 XML.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
XML Instructor: Charles Moen CSCI/CINF XML  Extensible Markup Language  A set of rules that allow you to create your own markup language  Designed.
XP 1 Creating an XML Document Developing an XML Document for the Jazz Warehouse XML Tutorial.
Lecture 16 Introduction to XML Boriana Koleva Room: C54
Accessing Data Using XML CHAPTER NINE Matakuliah: T0063 – Pemrograman Visual Tahun: 2009.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Understanding How XML Works Ellen Pearlman Eileen Mullin Programming the.
1 Tutorial 11 Creating an XML Document Developing a Document for a Cooking Web Site.
Tutorial 13 Validating Documents with Schemas
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 4 1COMP9321, 15s2, Week.
Chapter 15: XML TP2543 Web Programming Mohammad Faidzul Nasrudin.
XML CSC1310 Fall HTML (TIM BERNERS-LEE) HyperText Markup Language  HTML (HyperText Markup Language): December  Markup  Markup is a symbol.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
 XML derives its strength from a variety of supporting technologies.  Structure and data types: When using XML to exchange data among clients, partners,
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
CHAPTER NINE Accessing Data Using XML. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Introduction The eXtensible.
XML Introduction to XML Extensible Markup Language.
XML Schema – XSLT Week 8 Web site:
1 XML and RSS. 2 Knowing trees, I understand the meaning of patience. Knowing grass, I can appreciate persistence. — Hal Borland Like everything metaphysical,
XML Notes taken from w3schools. What is XML? XML stands for EXtensible Markup Language. XML was designed to store and transport data. XML was designed.
Unit 4 Representing Web Data: XML
Extensible Markup Language XML
Chapter 7 Representing Web Data: XML
14 XML.
Presentation transcript:

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

 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

 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

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

 2006 Pearson Education, Inc. All rights reserved 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 (Optional) XSLT with Class XslCompiledTransform Wrap-Up Web Resources

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

 2006 Pearson Education, Inc. All rights reserved 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

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

 2006 Pearson Education, Inc. All rights reserved 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

 2006 Pearson Education, Inc. All rights reserved 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

 2006 Pearson Education, Inc. All rights reserved 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

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

 2006 Pearson Education, Inc. All rights reserved 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

 2006 Pearson Education, Inc. All rights reserved 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

 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

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

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

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

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

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

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

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

 2006 Pearson Education, Inc. All rights reserved 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

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

 2006 Pearson Education, Inc. All rights reserved 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

 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

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

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

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

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

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

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

 2006 Pearson Education, Inc. All rights reserved 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”

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

 2006 Pearson Education, Inc. All rights reserved 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

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

 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

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

 2006 Pearson Education, Inc. All rights reserved 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

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

 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., ).

 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

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

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

 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).

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

 2006 Pearson Education, Inc. All rights reserved 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

 2006 Pearson Education, Inc. All rights reserved 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

 2006 Pearson Education, Inc. All rights reserved. 49 Outline book.xml Indicates that the books element is a part of the namespace

 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

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

 2006 Pearson Education, Inc. All rights reserved. 52 Portability Tip 19.3 W3C XML Schema authors specify URI 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.

 2006 Pearson Education, Inc. All rights reserved 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

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

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

 2006 Pearson Education, Inc. All rights reserved 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

 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

 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

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

 2006 Pearson Education, Inc. All rights reserved (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

 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

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

 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).

 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

 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

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

 2006 Pearson Education, Inc. All rights reserved. 67 Outline sorting.xsl (1 of 4) Matches the root element of the document in Fig 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

 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

 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

 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

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

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

 2006 Pearson Education, Inc. All rights reserved (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

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

 2006 Pearson Education, Inc. All rights reserved (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

 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

 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

 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

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

 2006 Pearson Education, Inc. All rights reserved (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

 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

 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

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

 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

 2006 Pearson Education, Inc. All rights reserved (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

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

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

 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

 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

 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

 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

 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

 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

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

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

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

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

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

 2006 Pearson Education, Inc. All rights reserved (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

 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

 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

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

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

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

 2006 Pearson Education, Inc. All rights reserved (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

 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

 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

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