(Optional) Extensible Stylesheet Language and XSL Transformations

Extensible Stylesheet Language (XSL) documents specify how programs are to render XML document data. XSL is a group of three technologiesXSL-FO (XSL Formatting Objects), XPath (XML Path Language) and XSLT (XSL Transformations). XSL-FO is a vocabulary for specifying formatting, and XPath is a string-based language of expressions used by XML and many of its related technologies for effectively and efficiently locating structures and data (such as specific elements and attributes) in XML documents.

The third portion of XSLXSL Transformations (XSLT)is a technology for transforming XML documents into other documentsi.e., transforming the structure of the XML document data to another structure. XSLT provides elements that define rules for transforming one XML document to produce a different XML document. This is useful when you want to use data in multiple applications or on multiple platforms, each of which may be designed to work with documents written in a particular vocabulary. For example, XSLT allows you to convert a simple XML document to an XHTML (Extensible HyperText Markup Language) document that presents the XML document's data (or a subset of the data) formatted for display in a Web browser. (See Fig. 19.16 for a sample "before" and "after" view of such a transformation.) XHTML is the W3C technical recommendation that replaces HTML for marking up Web content. For more information on XHTML, see Appendix F, Introduction to XHTML: Part 1, and Appendix G, Introduction to XHTML: Part 2, and visit www.w3.org.

Figure 19.16. XML document that describes various sports.

1 5 6 7 8 "783"> 9 Cricket 10 11 12 More popular among commonwealth nations. 13 14 15 16 "239"> 17 Baseball 18 19 20 More popular in America. 21 22 23 24 "418"> 25 Soccer (Futbol) 26 27 28 Most popular sport in the world. 29 30 31  

Transforming an XML document using XSLT involves two tree structuresthe source tree (i.e., the XML document to be transformed) and the result tree (i.e., the XML document to be created). XPath is used to locate parts of the source tree document that match templates defined in an XSL style sheet. When a match occurs (i.e., a node matches a template), the matching template executes and adds its result to the result tree. When there are no more matches, XSLT has transformed the source tree into the result tree. The XSLT does not analyze every node of the source tree; it selectively navigates the source tree using XPath's select and match attributes. For XSLT to function, the source tree must be properly structured. Schemas, DTDs and validating parsers can validate document structure before using XPath and XSLTs.

A Simple XSL Example

Figure 19.16 lists an XML document that describes various sports. The output shows the result of the transformation (specified in the XSLT template of Fig. 19.17) rendered by Internet Explorer 6.

Figure 19.17. XSLT that creates elements and attributes in an XHTML document.

(This item is displayed on page 957 in the print version)

1 3 4 5 6 "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 "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 "/"> 15 16

"http://www.w3.org/1999/xhtml"> 17 18 Sports 19 20 21 22 "1"bgcolor ="wheat">2324252627282930313233 "/sports/game"> 34 353637383940
ID Sport Information
"@id"/> "name"/> "paragraph"/>
41 42 43 44 45

To perform transformations, an XSLT processor is required. Popular XSLT processors include Microsoft's MSXML and the Apache Software Foundation's Xalan 2 (xml.apache.org). The XML document shown in Fig. 19.16 is transformed into an XHTML document by MSXML when the document is loaded in Internet Explorer. MSXML is both an XML parser and an XSLT processor.

Line 2 (Fig. 19.16) is a processing instruction (PI) that references the XSL style sheet sports.xsl (Fig. 19.17). A processing instruction is embedded in an XML document and provides application-specific information to whichever XML processor the application uses. In this particular case, the processing instruction specifies the location of an XSLT document with which to transform the XML document. The characters 3 4 5 3 4 5 "1.0" 6 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 7 8 9 "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 14 "/"> 15 "http://www.w3.org/1999/xhtml"> 16 17 18 19 20 21 "book"> 22 23 ISBN "@isbn"/> - 24 "title"/> 25 26 27 28

"color: blue"> "title"/>

29

"color: blue">by 30 "author/lastName"/>, 31 "author/firstName"/>

32 33 = "border-style: groove; background-color: wheat">3435 "chapters/frontMatter/*"> 36 37404144454647 "chapters/chapter"> 48 "@number" data-type = "number" 49 order = "ascending"/> 50 51545559606162 "chapters/appendix"> 63 "@number" data-type = "text" 64 order = "ascending"/> 65 666970747576

"text-align: right"> 38 "name()"/> 39 42 ( "@pages"/> pages ) 43
"text-align: right"> 52 Chapter "@number"/> 53 56 "text()"/> 57 ( "@pages"/> pages ) 58
"text-align: right"> 67 Appendix "@number"/> 68 71 "text()"/> 72 ( "@pages"/> pages ) 73
77 78

Pages: 79 "pagecount" 80 select = "sum(chapters//*/@pages)"/> 81 "$pagecount"/> 82

Media Type: "media/@type"/>

83 84 85

Lines 2324 create the title for the XHTML document. We use the book's ISBN (from attribute isbn) and the contents of element title to create the string that appears in the browser window's title bar (ISBN 999-99999-9-X - Deitel's XML Primer).

Line 28 creates a header element that contains the book's title. Lines 2931 create a header element that contains the book's author. Because the context node (i.e., the current node being processed) is book, the XPath expression author/lastName selects the author's last name, and the expression author/firstName selects the author's first name.

Line 35 selects each element (indicated by an asterisk) that is a child of element frontMatter. Line 38 calls node-set function name to retrieve the current node's element name (e.g., preface). The current node is the context node specified in the xsl:for-each (line 35). Line 42 retrieves the value of the pages attribute of the current node.

Line 47 selects each chapter element. Lines 4849 use element xsl:sort to sort chapters by number in ascending order. Attribute select selects the value of attribute number in context node chapter. Attribute data-type, with value "number", specifies a numeric sort, and attribute order, with value "ascending", specifies ascending order. Attribute data-type also accepts the value "text" (line 63), and attribute order also accepts the value "descending". Line 56 uses node-set function text to obtain the text between the chapter start and end tags (i.e., the name of the chapter). Line 57 retrieves the value of the pages attribute of the current node. Lines 6275 perform similar tasks for each appendix.

Lines 7980 use an XSL variable to store the value of the book's total page count and output the page count to the result tree. Attribute name specifies the variable's name (i.e., pagecount), and attribute select assigns a value to the variable. Function sum (line 80) totals the values for all page attribute values. The two slashes between chapters and * indicate a recursive descentthe MSXML processor will search for elements that contain an attribute named pages in all descendant nodes of chapters. The XPath expression

//*

 

selects all the nodes in an XML document. Line 81 retrieves the value of the newly created XSL variable pagecount by placing a dollar sign in front of its name.

Summary of XSL Style Sheet Elements

This section's examples used several predefined XSL elements to perform various operations. Figure 19.20 lists these elements and several other commonly used XSL elements. For more information on these elements and XSL in general, see www.w3.org/Style/XSL.

Figure 19.20. XSL style sheet elements.

Element

Description

Applies the templates of the XSL document to the children of the current node.

   

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.

Contains rules to apply when a specified node is matched.

   

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.

   

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

   

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

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 elementit can be used only as a child element of an xml:stylesheet.

Adds the current node to the output tree.

 

This section introduced Extensible Stylesheet Language (XSL) and showed how to create XSL transformations to convert XML documents from one format to another. We showed how to transform XML documents to XHTML documents for display in a Web browser. Recall that these transformations are performed by MSXML, Internet Explorer's built-in XML parser and XSLT processor. In most business applications, XML documents are transferred between business partners and are transformed to other XML vocabularies programmatically. In Section 19.10, we demonstrate how to perform XSL transformations using the XslCompiledTransform class provided by the .NET Framework.

Категории