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 technologies—XSL-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 XSL—XSL Transformations (XSLT)—is a technology for trans-forming XML documents into other documents—i.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 document that presents the XML document’s data (or a subset of the data) formatted for display in a web browser.
Transforming an XML document using XSLT involves two tree structures—the 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 14.20 lists an XML document that describes various sports. The output shows the result of the transformation (specified in the XSLT template of Fig. 14.21) rendered by Internet Explorer.
To perform transformations, an XSLT processor is required. Popular XSLT proces-sors include Microsoft’s MSXML and the Apache Software Foundation’s Xalan 2 (xml.apache.org). The XML document in Fig. 14.20 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. Firefox also includes an XSLT processor.
1 <?xml version = "1.0"?>
2 <?xml-stylesheet type = "text/xsl" href = "sports.xsl"?>
4 <!-- Fig. 14.20: sports.xml -->
5 <!-- Sports Database -->
8 <game id = "783">
12 More popular among commonwealth nations.
16 <game id = "239">
20 More popular in America.
24 <game id = "418">
25 <name>Soccer (Futbol)</name>
28 Most popular sport in the world.
Fig. 14.20 | XML document that describes various sports.
Line 2 (Fig. 14.20) is a processing instruction (PI) that references the XSL style sheet sports.xsl (Fig. 14.21). 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 <? and ?> (line 2, Fig. 14.20) delimit a processing instruction, which consists of a PI target (e.g., xml-stylesheet) and a PI value (e.g., type = "text/xsl" href = "sports.xsl"). The PI value’s type attribute specifies that sports.xsl is a text/xsl file (i.e., a text file con-taining XSL content). The href attribute specifies the name and location of the style sheet to apply—in this case, sports.xsl in the current directory.
Figure 14.21 shows the XSL document for transforming the structured data of the XML document of Fig. 14.20 into an XHTML document for presentation. By conven-tion, XSL documents have the filename extension .xsl.
Lines 6–7 begin the XSL style sheet with the stylesheet start tag. Attribute version specifies the XSLT version to which this document conforms. Line 7 binds namespace prefix xsl to the W3C’s XSLT URI (i.e., http://www.w3.org/1999/XSL/Transform).
1 <?xml version = "1.0"?>
2 <!-- Fig. 14.21: sports.xsl -->
3 <!-- A simple XSLT transformation -->
5 <!-- reference XSL style sheet URI -->
6 <xsl-stylesheet version = "1.0"
7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
9 <xsl:output method = "html" omit-xml-declaration = "no"
10 doctype-system =
12 doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"/>
14 <xsl:template match = "/"> <!-- match root element -->
16 <html xmlns = "http://www.w3.org/1999/xhtml">
22 <table border = "1" bgcolor = "wheat">
31 <!-- insert each name and paragraph element value -->
32 <!-- into a table row. -->
33 <xsl:for-each select = "/sports/game">
35 <td><xsl:value-of select = "@id"/></td>
36 <td><xsl:value-of select = "name"/></td>
37 <td><xsl:value-of select = "paragraph"/></td>
Fig. 14.21 | XSLT that creates elements and attributes in an XHTML document.
Lines 9–12 use element xsl:output to write an XHTML document type declaration (DOCTYPE) to the result tree (i.e., the XML document to be created). The DOCTYPE identi-fies XHTML as the type of the resulting document. Attribute method is assigned "html", which indicates that HTML is being output to the result tree. Attribute omit-xml-dec-laration specifies whether the transformation should write the XML declaration to the result tree. In this case, we do not want to omit the XML declaration, so we assign to this attribute the value "no". Attributes doctype-system and doctype-public write the DOC-TYPE DTD information to the result tree.
XSLT uses templates (i.e., xsl:template elements) to describe how to transform par-ticular nodes from the source tree to the result tree. A template is applied to nodes that are specified in the required match attribute. Line 14 uses the match attribute to select the doc-ument root (i.e., the conceptual part of the document that contains the root element and everything below it) of the XML source document (i.e., sports.xml). The XPath char-acter / (a forward slash) always selects the document root. Recall that XPath is a string-based language used to locate parts of an XML document easily. In XPath, a leading for-ward slash specifies that we are using absolute addressing (i.e., we are starting from the root and defining paths down the source tree). In the XML document of Fig. 14.20, the child nodes of the document root are the two processing instruction nodes (lines 1–2), the two comment nodes (lines 4–5) and the sports element node (lines 7–31). The template in Fig. 14.21, line 14, matches a node (i.e., the root node), so the contents of the template are now added to the result tree.
The MSXML processor writes the XHTML in lines 16–29 (Fig. 14.21) to the result tree exactly as it appears in the XSL document. Now the result tree consists of the DOCTYPE definition and the XHTML code from lines 16–29. Lines 33–39 use element xsl:for-each to iterate through the source XML document, searching for game elements. Attribute select is an XPath expression that specifies the nodes (called the node set) on which the xsl:for-each operates. Again, the first forward slash means that we are using absolute addressing. The forward slash between sports and game indicates that game is a child node of sports. Thus, the xsl:for-each finds game nodes that are children of the sports node. The XML document sports.xml contains only one sports node, which is also the docu-ment root node. After finding the elements that match the selection criteria, the xsl:for-each processes each element with the code in lines 34–38 (these lines produce one row in a table each time they execute) and places the result of lines 34–38 in the result tree.
Line 35 uses element value-of to retrieve attribute id’s value and place it in a td ele-ment in the result tree. The XPath symbol @ specifies that id is an attribute node of the context node game. Lines 36–37 place the name and paragraph element values in td ele-ments and insert them in the result tree. When an XPath expression has no beginning for-ward slash, the expression uses relative addressing. Omitting the beginning forward slash tells the xsl:value-of select statements to search for name and paragraph elements that are children of the context node, not the root node. Due to the last XPath expression selec-tion, the current context node is game, which indeed has an id attribute, a name child ele-ment and a paragraph child element.
Using XSLT to Sort and Format Data
Figure 14.22 presents an XML document (sorting.xml) that marks up information about a book. Note that several elements of the markup describing the book appear out of
1 <?xml version = "1.0"?>
2 <?xml-stylesheet type = "text/xsl" href = "sorting.xsl"?>
4 <!-- Fig. 14.22: sorting.xml -->
5 <!-- XML document containing book information -->
6 <book isbn = "999-99999-9-X">
7 <title>Deitel's XML Primer</title>
16 <preface pages = "2" />
17 <contents pages = "5" />
18 <illustrations pages = "4" />
21 <chapter number = "3" pages = "44">Advanced XML</chapter>
22 <chapter number = "2" pages = "35">Intermediate XML</chapter>
23 <appendix number = "B" pages = "26">Parsers and Tools</appendix>
24 <appendix number = "A" pages = "7">Entities</appendix>
25 <chapter number = "1" pages = "28">XML Fundamentals</chapter>
28 <media type = "CD" />
Fig. 14.22 | XML document containing book information.
order (e.g., the element describing Chapter 3 appears before the element describing Chap-ter 2). We arranged them this way purposely to demonstrate that the XSL style sheet ref-erenced in line 2 (sorting.xsl) can sort the XML file’s data for presentation purposes.
Figure 14.23 presents an XSL document (sorting.xsl) for transforming sorting.xml (Fig. 14.22) to XHTML. Recall that an XSL document navigates a source tree and builds a result tree. In this example, the source tree is XML, and the output tree is XHTML. Line 14 of Fig. 14.23 matches the root element of the document in Fig. 14.22. Line 15 outputs an html start tag to the result tree. In line 16, the <xsl:apply-templates/> element specifies that the XSLT processor is to apply the xsl:templates defined in this XSL document to the current node’s (i.e., the document root’s) children. The content from the applied templates is output in the html element that ends at line 17.
Lines 21–84 specify a template that matches element book. The template indicates how to format the information contained in book elements of sorting.xml (Fig. 14.22) as XHTML.
Lines 23–24 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 29–31 create a header element that contains the book’s author. Because the context node (i.e., the current
1 <?xml version = "1.0"?>
3 <!-- Fig. 14.23: sorting.xsl -->
4 <!-- Transformation of book information into XHTML -->
5 <xsl:stylesheet version = "1.0"
6 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
8 <!-- write XML declaration and DOCTYPE DTD information -->
9 <xsl:output method = "html" 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"/>
13 <!-- match document root -->
14 <xsl:template match = "/">
15 <html xmlns = "http://www.w3.org/1999/xhtml">
20 <!-- match book -->
21 <xsl:template match = "book">
23 <title>ISBN <xsl:value-of select = "@isbn"/> -
24 <xsl:value-of select = "title"/></title>
28 <h1 style = "color: blue"><xsl:value-of select = "title"/></h1>
<h2 style = "color: blue">by
30 <xsl:value-of select = "author/lastName"/>,
31 <xsl:value-of select = "author/firstName"/></h2>
33 <table style = "border-style: groove; background-color: wheat">
35 <xsl:for-each select = "chapters/frontMatter/*">
37 <td style = "text-align: right">
38 <xsl:value-of select = "name()"/>
42 ( <xsl:value-of select = "@pages"/> pages )
47 <xsl:for-each select = "chapters/chapter">
48 <xsl:sort select = "@number" data-type = "number"
49 order = "ascending"/>
51 <td style = "text-align: right">
52 Chapter <xsl:value-of select = "@number"/>
56 <xsl:value-of select = "text()"/>
57 ( <xsl:value-of select = "@pages"/> pages )
62 <xsl:for-each select = "chapters/appendix">
63 <xsl:sort select = "@number" data-type = "text"
64 order = "ascending"/>
66 <td style = "text-align: right">
67 Appendix <xsl:value-of select = "@number"/>
71 <xsl:value-of select = "text()"/>
72 ( <xsl:value-of select = "@pages"/> pages )
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>
Fig. 14.23 | XSL document that transforms sorting.xml into XHTML
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 48–49 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 62–75 perform similar tasks for each appendix.
Lines 79–80 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 descent—the 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 opera-tions. Figure 14.24 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.
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.