940 likes | 1.18k Views
19. XML and LINQ to XML. 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
E N D
19 • XML and LINQto XML
Knowing trees, I understand the meaning ofpatience. Knowing grass, I can appreciate persistence. Hal Borland Like everything metaphysical, the harmonybetween thought and reality is to be found inthe grammar of the language. Ludwig Wittgenstein 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
OBJECTIVES In this chapter you will learn: • 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 use XML axis properties to accessdata in XML documents.
OBJECTIVES • To use XML literals and embeddedexpressions to create XML documentsdirectly in Visual Basic code. • To use LINQ to extract and manipulate data from XML documents. • To transform XML documents into XHTML using class XslCompiledTransform.
19.1 Introduction • 19.2 Document Type Definitions (DTDs) • 19.3 W3C XML Schema Documents • 19.4 Extensible Stylesheet Language and XSL Transformations • 19.5 LINQ to XML: Document Object Model (DOM) • 19.6 LINQ to XML: XML Axis Properties • 19.7 LINQ to XML: XML Literals and Embedded Expressions • 19.8 XSLT with Class XslCompiledTransform
19.1 Introduction • ExtensibleMarkup Language(XML) was developed in 1996 by the W3C. • XML is a standard for describing data for exchange between systems. • Visual Studio uses XML extensively for project files.
19.2 Document Type Definitions (DTDs) Software Engineering Observation 19.1 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 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.
19.2 Document Type Definitions (DTDs) (Cont.) Software Engineering Observation 19.2 Many organizations and individuals arecreating DTDs and schemas for a broad rangeof applications.
A DTD (Fig. 19.1) describes the structure of an XMLdocument. • The document structure uses EBNF (ExtendedBackus-Naur Form) grammar. Outline letter.dtd ( 1 of 2 ) TheELEMENTelement type declarationdefines the rules for its element. The ATTLISTattribute-list declaration defines attributes. EMPTY specifies that the element does not contain any data between its start and end tags. Fig. 19.1 |Document Type Definition (DTD) for a business letter. (Part 1 of 2.)
Outline Specify that the value must be one of the enumerated values with a vertical bar meaning “or.” letter.dtd ( 2 of 2 ) Fig. 19.1 |Document Type Definition (DTD) for a business letter. (Part 2 of 2.) Common Programming Error 19.1 For documents validated with DTDs, any document that uses elements, attributes or relationships not explicitly defined by a DTD is an invalid document .
19.2 Document Type Definitions (DTDs) (Cont.) • The ELEMENTelement type declarationdefines the rules for its element. <!ELEMENT letter ( contact+, salutation, paragraph+, closing, signature )> • The plus sign (+) occurrence indicatorallows one or more of an element. • Theasterisk(*)indicates an optional element. • Thequestion mark(?) indicates an element that may occur once. • By default, the DTD allows exactly one occurrence.
19.2 Document Type Definitions (DTDs) (Cont.) • The ATTLISTattribute-list declarationdefines attributes. <!ATTLIST contact type CDATA #IMPLIED> • #IMPLIEDspecifies that the attribute is optional. • #REQUIRED specifies that the attribute must be present. • #FIXED specifies that the attribute must have the given value. <!ATTLIST address zip CDATA#FIXED"01757"> • CDATAspecifies that attribute type contains character data (i.e., a string). • #PCDATAspecifies that an element may contain parsed character data.
19.2 Document Type Definitions (DTDs) (Cont.) Software Engineering Observation 19.3 DTD syntax cannot describe an element’s(or attribute’s) type. For example, a DTD cannot specify that a particular element or attribute can contain only integer data.
19.2 Document Type Definitions (DTDs) (Cont.) Common Programming Error 19.2 Using markup characters (e.g., <, >and&) in parsed character data is an error. Use character entity references (e.g., <,>and &instead).
19.2 Document Type Definitions (DTDs) (Cont.) • EMPTY specifies that the element does not contain any data between its start and end tags. • Specify that the value must be one of the enumerated values with a vertical bar meaning “or.” <!ATTLIST flag gender (M | F) "M"> • Note that gender has a default value of M.
19.2 Document Type Definitions (DTDs) (Cont.) • Open the XML file in Visual Studio as in Fig. 19.2. Fig. 19.2 | An XML file open in Visual Basic 2008 Express Edition.
19.3 W3C XML Schema Documents • DTDs lack a way of indicating what specific type of data an element can contain. • Schemas are an XML alternative to DTDs. • An XML document that conforms to a schema document isschema valid.
Outline • Figure 19.3 shows a schema-valid XML documentnamed book.xml. book.xml ( 1 of 2 ) The namespace is declared and given the prefix deitel. Fig. 19.3 |Schema-valid XML document describing a list of books. (Part 1 of 2.)
Outline book.xml ( 2 of 2 ) Fig. 19.3 |Schema-valid XML document describing a list of books. (Part 2 of 2.)
Outline • Fig. 19.4 shows the XML Schema document(book.xsd) that defines the structure for book.xml. book.xsd ( 1 of 2 ) Schema namespace declarations. The targetNamespace attribute identifies the namespace of the XML vocabulary that this schema defines. The element tag with attributes name and type defines an element. Defines the complex type BooksType. Attributes minOccurs and maxOccurs specify the number required. Fig. 19.4 |XML Schema document for book.xml. (Part 1 of 2.)
Outline book.xsd ( 2 of 2 ) Opening the Schema file with Internet Explorer Fig. 19.4 |XML Schema document for book.xml. (Part 2 of 2.)
19.3 W3C XML Schema Documents (Cont.) • The schema defines elements, attributes and parent-child relationships. • The schema also specifies data types. • The targetNamespace attribute identifies the namespace of the XML vocabulary that this schema defines.
19.3 W3C XML Schema Documents (Cont.) Portability Tip 19.1 The URI http://www.w3.org/2001/XMLSchema namespace contains elements that comprise the XML Schema vocabulary.
19.3 W3C XML Schema Documents (Cont.) • The element tag with attributes name and type defines an element. <element name="book“ type="deitel:SingleBookType" /> • Types include XML Schema–defined types(e.g., string, double) and user-defined types. • Element types can be simple types or complex types. • Simple types cannot contain attributes or child elements.
19.3 W3C XML Schema Documents (Cont.) • The sequenceelement specifies a sequential order for child elements. • Attributes minOccursandmaxOccurs specify the number required. • unboundedspecifies any number of book child elements.
19.3 W3C XML Schema Documents (Cont.) Fig. 19.5|Some XML Schema built-in types. (Part 1 of 3.)
19.3 W3C XML Schema Documents (Cont.) Fig. 19.5|Some XML Schema built-in types. (Part 2 of 3.)
19.3 W3C XML Schema Documents (Cont.) Fig. 19.5|Some XML Schema built-in types. (Part 3 of 3.)
Outline • The schema document in Fig. 19.6 has both simpleand complex types. computer.xsd ( 1 of 2 ) Simple types are derived from a base type. The minInclusive element restricts accepted values. Fig. 19.6 |XML Schema document defining simple and complex types. (Part 1 of 2.)
Outline computer.xsd ( 2 of 2 ) encloses elements that must each be included once. Defining an attribute. Fig. 19.6 |XML Schema document defining simple and complex types. (Part 2 of 2.)
19.3 W3C XML Schema Documents (Cont.) • Simple types are derived from a base type. • The minInclusive element restricts accepted values. • <all> encloses elements that must each be included once.
Outline • The XML document in Fig. 19.7 follows the structuredefined in Fig. 19.6. laptop.xml ( 1 of 1 ) Fig. 19.7 |XML document using the laptop element defined in computer.xsd Good Programming Practice 19.1 Having Visual Studio automatically generate a schema is a good starting point, but it is usually necessary to modify it to refine the restrictions and specified types.
Outline • Extensible Stylesheet Language (XSL)documentsspecify how programs should render XML documentdata. • Figure 19.8 lists an XML document thatdescribes various sports. sports.xml ( 1 of 2 ) A processing instruction(PI) references the XSL style sheet Fig. 19.8 |XML document that describes various sports. (Part 1 of 2.)
Outline sports.xml ( 2 of 2 ) Opening the XML document in Internet Explorer Fig. 19.8 |XML document that describes various sports. (Part 2 of 2.)
19.4 Extensible Stylesheet Language and XSL Transformations (Cont.) • A processing instruction(PI) references the XSL style sheet <?xml-stylesheet type = "text/xsl"href ="sports.xsl"?> • The processing instruction consists of a PI target (xml-stylesheet) and a PI value (type="text/xsl"href="sports.xsl").
Outline • XSL Transformation (Fig. 19.9) defines rules fortransforming one XML document to produce adifferent XML document. • The output shows the result of the transformationrendered by Internet Explorer. sports.xsl ( 1 of 3 ) Beginning with the stylesheet start tag and the XSLT URI. Use element xsl:outputto write an XHTML document type declaration (DOCTYPE) to the result tree. XSLT uses xsl:templates to describe how to transform particular nodes from the source tree to the result tree. Fig. 19.9 |XSLT that creates elements and attributes in anXHTML document. (Part 1 of 3.)
Outline sports.xsl ( 2 of 3 ) Fig. 19.9 |XSLT that creates elements and attributes in anXHTML document. (Part 2 of 3.)
Outline sports.xsl ( 3 of 3 ) xsl:for-each iterates through elements in the source XML document. Attribute select specifies the nodes on which the xsl:for-each operates. Fig. 19.9 |XSLT that creates elements and attributes in anXHTML document. (Part 3 of 3.) Software Engineering Observation 19.4 .XSL enables document authors to separate data presentation (specified in XSL documents) from data description (specifiedin XML documents).
19.4 Extensible Stylesheet Language and XSL Transformations (Cont.) • Use element xsl:output to write an XHTML document type declaration (DOCTYPE) to the result tree. • XSLT uses xsl:templates to describe how to transform particular nodes from the source tree to the result tree.
19.4 Extensible Stylesheet Language and XSL Transformations (Cont.) • The XPath character / selects the document root. • Starting with a forward slash specifies absolute addressing (i.e., starting from the root and defining paths down the source tree). • When an XPath expression has no beginning forward slash, the expression uses relative addressing.
19.4 Extensible Stylesheet Language and XSL Transformations (Cont.) • xsl:for-each iterates through elements in the source XML document. • Attribute select specifies the nodes on which the xsl:for-each operates. • value-of retrieves an attribute’s value.
Outline • Figure 19.10 presents an XML document withinformation about a book. sorting.xml ( 1 of 2 ) The XSL style sheet for the XML document is referenced. Fig. 19.10 |XML document containing book information. (Part 1 of 2.)
Outline sorting.xml ( 2 of 2 ) Fig. 19.10 |XML document containing book information. (Part 2 of 2.)
Outline • Figure 19.11 presents an XSL document for transforming sorting.xml to XHTML. sorting.xsl ( 1 of 5 ) Defining an XSL document. Matching the root element of the target XML document. Fig. 19.11 |XSL document that transforms sorting.xmlinto XHTML. (Part 1 of 5.)
Outline Specifying a template that matches element book. sorting.xsl ( 2 of 5 ) Creating the title for the XHTML document from the book’s ISBN. Creating a header element with the book’s author’s name. Calling node-set function name retrieves the current node’s element name. Fig. 19.11 |XSL document that transforms sorting.xmlinto XHTML. (Part 2 of 5.)
Outline sorting.xsl ( 3 of 5 ) Retrieving the value of the pages attribute of the current node. xsl:sort sorts nodes according to criteria in the attributes. Calling node-set function text obtains the text between the chapter start and end tags. Fig. 19.11 |XSL document that transforms sorting.xmlinto XHTML. (Part 3 of 5.)
Outline sorting.xsl ( 4 of 5 ) Retrieving the value of pagecount by placing a dollar sign in front of its name. Fig. 19.11 |XSL document that transforms sorting.xmlinto XHTML. (Part 4 of 5.)
Outline sorting.xsl ( 5 of 5 ) Fig. 19.11 |XSL document that transforms sorting.xmlinto XHTML. (Part 5 of 5.)
19.4 Extensible Stylesheet Language and XSL Transformations (Cont.) • xsl:textis used to insert literal text. • Calling node-set function name retrieves the current node’s element name. • Calling node-set function text obtains the text between the chapter start and end tags. • xsl:sort sorts nodes according to criteria in the attributes.
19.4 Extensible Stylesheet Language and XSL Transformations (Cont.) • An XSL variable stores a value obtained from the XML document. • The following XPath expression selects all the nodes in an XML document. //* Performance Tip 19.1 Selecting all nodes in a document when it is not necessary slows XSLT processing.