(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  "1.0"?>
 2  "text/xsl" href = "sports.xsl"?>
 8  "783">
 9 Cricket
12 More popular among commonwealth nations.
16  "239">
17 Baseball
20 More popular in America.
24  "418">
25 Soccer (Futbol)
28 Most popular sport in the world.

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  "1.0"?>
 6  "1.0" 
 7  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
 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"/> 
14  "/"> 
"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 and ?> (line 2, Fig. 19.16) 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 containing XSL content). The href attribute specifies the name and location of the style sheet to applyin this case, sports.xsl in the current directory.

Software Engineering Observation 19 5

XSL enables document authors to separate data presentation (specified in XSL documents) from data description (specified in XML documents).


Figure 19.17 shows the XSL document for transforming the structured data of the XML document of Fig. 19.16 into an XHTML document for presentation. By convention, XSL documents have the filename extension .xsl.

Lines 67 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).

Lines 912 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 identifies XHTML as the type of the resulting document. Attribute method is assigned "xml", which indicates that XML is being output to the result tree. (Recall that XHTML is a type of XML.) Attribute omit-xml-declaration 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 DOCTYPE DTD information to the result tree.

XSLT uses templates (i.e., xsl:template elements) to describe how to transform particular 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 document 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 character / (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 forward 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. 19.16, the child nodes of the document root are the two processing instruction nodes (lines 12), the two comment nodes (lines 45) and the sports element node (lines 731). The template in Fig. 19.17, 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 1629 (Fig. 19.17) 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 1629. Lines 3339 use element xsl:foreach to iterate through the source XML document, searching for game elements. The xsl:for-each element is similar to C#'s foreach statement. 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 document root node. After finding the elements that match the selection criteria, the xsl:for-each processes each element with the code in lines 3438 (these lines produce one row in a table each time they execute) and places the result of lines 3438 in the result tree.

Line 35 uses element value-of to retrieve attribute id's value and place it in a td element in the result tree. The XPath symbol @ specifies that id is an attribute node of the context node game. Lines 3637 place the name and paragraph element values in TD elements and insert them in the result tree. When an XPath expression has no beginning forward 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 selection, the current context node is game, which indeed has an id attribute, a name child element and a paragraph child element.

Using XSLT to Sort and Format Data

Figure 19.18 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 order (e.g., the element describing Chapter 3 appears before the element describing Chapter 2). We arranged them this way purposely to demonstrate that the XSL style sheet referenced in line 5 (sorting.xsl) can sort the XML file's data for presentation purposes.

Figure 19.18. XML document containing book information.

 1  "1.0"?>
 5  "text/xsl" href = "sorting.xsl"?>
 7  "999-99999-9-X">
Deitel's XML Primer 9 10 11 Jane 12 Blue 13 14 15 16 17 "2" /> 18 "5" /> 19 "4" /> 20 21 22 "3" pages = "44">Advanced XML 23 "2" pages = "35">Intermediate XML 24 "B" pages = "26">Parsers and Tools 25 "A" pages = "7">Entities 26 "1" pages = "28">XML Fundamentals 27 28 29 "CD" /> 30

Figure 19.19 presents an XSL document (sorting.xsl) for transforming sorting.xml (Fig. 19.18) 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. 19.19 matches the root element of the document in Fig. 19.18. Line 15 outputs an html start tag to the result tree. The element (line 16) 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 2184 specify a template that matches element book. The template indicates how to format the information contained in book elements of sorting.xml (Fig. 19.18) as XHTML.

Figure 19.19. XSL document that transforms sorting.xml into XHTML.

 1  "1.0"?>
 5  "1.0" 
 6  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
 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"/>
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"/>


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



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.



    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look


    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2


    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures



    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net