Unit 12 XSL (XML Style Sheet Language) : Structure
Unit 12 XSL (XML Style Sheet Language) : Structure
Unit 12 XSL (XML Style Sheet Language) : Structure
Unit 12
Unit 12
Structure: 12.1 Introduction Objectives 12.2 12.3 12.4 12.5 12.6 12.7 12.8 12.9 XSLT XSLT Browsers
Correct Style Sheet Declaration XSLT <xsl:template> XSLT <xsl:value-of> Element XSLT <xsl:for-each> Element Filtering the Output XSLT <xsl:sort> Element
12.10 XSLT <xsl:if> Element 12.11 XSLT <xsl:choose> Element 12.12 XSLT <xsl:apply-templates> Element 12.13 XPath 12.14 Summary 12.15 Terminal Questions
12.1 Introduction
XSL stands for Extensible Stylesheet Language. XSL markup language is used to transform data from one markup language to other. XSLT (XSL Transformation Engine) is an implementation of XSL markup language. The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.
Unit 12
CSS = HTML Style Sheets HTML uses predefined tags and the meanings of the tags are well understood. The <table> element in HTML defines a table - and a browser knows how to display it. Adding styles to HTML elements is simple. Telling a browser to display an element in a special font or color is easy with CSS. XSL = XML Style Sheets XML does not use predefined tags (we can use any tag-names we like), and the meaning of these tags are not well understood. A <table> element could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it. XSL describes how the XML document should be displayed! XSL - More Than a Style Sheet Language XSL consists of three parts:
XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents
Objectives XSL is the Extensible Stylesheet Language. XSL transforms and translates XML data from one XML format into another. Consider, for example, that the same XML document may need to be displayed in HTML, PDF, and Postscript form. Without XSL, the XML document would have to be manually duplicated, and then converted into each of these three formats. Instead, XSL provides a mechanism of defining stylesheets to accomplish these types of tasks. Rather than having to change the data because of a different representation, XSL provides a complete separation of data, or
Sikkim Manipal University Page No. 228
Unit 12
content, and presentation. If an XML document needs to be mapped to another representation, then XSL is an excellent solution. It provides a method comparable to writing a Java program to translate data into a PDF or HTML document, but supplies a standard interface to accomplish the task. To perform the translation, an XSL document can contain formatting objects. These formatting objects are specific named tags that can be replaced with appropriate content for the target document type. A common formatting object might define a tag that some processor uses in the transformation of an XML document into PDF; in this case, the tag would be replaced by PDF specific information. Formatting objects are specific XSL instructions, and although we will lightly discuss them, they are largely beyond the scope of this book. Instead, we will focus more on XSLT, a completely text-based transformation process. Through the process of XSLT (Extensible Style sheet Language Transformation), an XSL textual style sheet and a textual XML document is "merged" together, and what results is the XML data formatted according to the XSL stylesheet.
12.2 XSLT
What is XSLT?
XSLT stands for XSL Transformations XSLT is the most important part of XSL XSLT transforms an XML document into another XML document XSLT uses XPath to navigate in XML documents XSLT is a W3C Recommendation
XSLT = XSL Transformations: XSLT is the most important part of XSL. XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an XHTML element. With XSLT you can add/remove elements and
Sikkim Manipal University Page No. 229
Unit 12
attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more. A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree. XSLT Uses XPath: XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.
Unit 12
Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used! The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> (or) <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document. The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0". Start with a Raw XML Document We want to transform the following XML document ("cdcatalog.xml") into XHTML: <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>JAVA</title> <artist>Balaguruswamy</artist> <country>India</country> <company>Hyderabad</company> <price>100.00</price> <year>1990</year> </cd> <cd>
Sikkim Manipal University Page No. 231
Unit 12
<title>java</title> <artist>schild</artist> <country>AP</country> <company>Madras</company> <price>120.00</price> <year>2000</year> </cd> </catalog> Viewing XML Files in Fire fox and Internet Explorer: Open the XML file (typically by clicking on a link) - The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu. Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements. Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be displayed as plain text. Create an XSL Style Sheet: Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body>
Sikkim Manipal University Page No. 232
Unit 12
<h2>My Books Collection</h2> <table border="1"> <tr bgcolor="red"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Link the XSL Style Sheet to the XML Document Add the XSL style sheet reference to your XML document ("cdcatalog.xml"): <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog> <cd> <title>JAVA</title> <artist>Balaguruswamy</artist> <country>India</country> <company>Hyderabad</company> <price>100.00</price> <year>1990</year>
Sikkim Manipal University Page No. 233
Unit 12
</cd> <cd> <title>java</title> <artist>schild</artist> <country>AP</country> <company>Madras</company> <price>120.00</price> <year>2000</year> </cd> </catalog> If you have an XSLT compliant browser it will nicely transform your XML into XHTML. The details of the example above will be explained in the next sections.
Unit 12
<xsl:template match="/"> <html> <body> <h2>My Books Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>,</td> <td>,</td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Since an XSL style sheet is an XML document itself, it always begins with the XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>. The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes). The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document. The content inside the <xsl:template> element defines some HTML to write to the output.
Unit 12
The last two lines define the end of the template and the end of the style sheet. The result of the transformation above will look like this: My Books Collection Title , Artist ,
The result from this example was a little disappointing, because no data was copied from the XML document to the output. In the next section you will learn how to use the <xsl:value-of> element to select values from the XML elements.
Unit 12
<td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories. The result of the transformation above will look like this: My Books Collction Title JAVA Artist Balaguruswamy
The result from this example was also a little disappointing, because only one line of data was copied from the XML document to the output. In the next section you will learn how to use the <xsl:for-each> element to loop through the XML elements, and display all of the records.
Unit 12
<xsl:template match="/"> <html> <body> <h2>My Books Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories. The result of the transformation above will look like this: My Books Collection Title JAVA Java Artist Balaguruswamy Schild
Page No. 238
Unit 12
Take a look at the adjusted XSL style sheet: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My Books Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table>
Sikkim Manipal University Page No. 239
Unit 12
</body> </html> </xsl:template> </xsl:stylesheet> The result of the transformation above will look like this: My Books Collection Title JAVA Artist Balaguruswamy
Unit 12
<td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Note: The select attribute indicates what XML element to sort on. The result of the transformation above will look like this: My Books Collection Title JAVA Java Artist Balaguruswamy Schild
Unit 12
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My Books Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Note: The value of the required test attribute contains the expression to be evaluated. The code above will only output the title and artist elements of the CDs that has a price that is higher than 10. The result of the transformation above will look like this:
Sikkim Manipal University Page No. 242
Unit 12
Unit 12
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> The code above will add a pink background-color to the "Artist" column WHEN the price of the CD is higher than 10.
Unit 12
The result of the transformation will look like this: My Books Collection Title JAVA Java Artist Balaguruswamy Schild
View the XML file, View the XSL file, and View the result Another Example Here is another example that contains two <xsl:when> elements: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My Books Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price > 9">
Sikkim Manipal University Page No. 245
Unit 12
<td bgcolor="#cccccc"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> The code above will add a pink background color to the "Artist" column WHEN the price of the CD is higher than 10, and a grey background-color WHEN the price of the CD is higher than 9 and lower or equal to 10. The result of the transformation will look like this: My Books Collection Title JAVA Java Artist Balaguruswamy Schild
View the XML file, View the XSL file, and View the result
Unit 12
can use the select attribute to specify the order in which the child nodes are processed. This element is evaluated in two steps: Step1: Locates the nodes from input xml document using the specified pattern. Step2: Evaluates the respective template for each of the selected node. While preparing a pattern we can use the following special characters: . (Dot): Describes the node for which this template/enclosing element is being evaluated. . . (Dot Dot): Describes the parent node of the current node. * (star): Describes all the child nodes. //: Describes the document node, this is used to specify the absolute node path. | (single pipe character): Describes or. NOTE: If a select attribute is not specified then it defaults to * i.e. it selects all the child nodes of the current node. Look at the following XSL style sheet: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My Books Collection</h2> <xsl:apply-templates/> </body> </html> </xsl:template>
Sikkim Manipal University Page No. 247
Unit 12
<xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet> The result of the transformation will look like this: My Books Collection Title: JAVA Artist: Balaguruswamy Title:Java Artist: Schild
12.13 XPath
XPath specification is a part of xml specification XPath specifications include some constructs to build paths, expressions and include some functions for string and operations. These specifications can be used in XSL.
Sikkim Manipal University Page No. 248
Unit 12
XPath is a major element in XSLT XPath is a W3C recommendation XPath Nodes In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XPath Axes This part of the XPath specifications includes keywords and functions that allow to build paths to locate nodes in an xml document. The XML Example Document We will use the following XML document in the examples below. <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>
Unit 12
Location Path Expression A location path can be absolute or relative. An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash: An absolute location path: /step/step/... A relative location path: step/step/... Each step is evaluated against the nodes in the current node-set.
Unit 12
an axis (defines the tree-relationship between the selected nodes and the current node)
a node-test (identifies a node within an axis) zero or more predicates (to further refine the selected node-set)
child::*/child::price
XPath Operators An XPath expression returns either a node-set, a string, a Boolean, or a number. Below is a list of the operators that can be used in XPath expressions:
Sikkim Manipal University Page No. 251
Unit 12
Operator |
Description Computes two nodesets Addition Subtraction Multiplication Division Equal Not equal Less than Less than or equal to Greater than Greater than or equal to or and Modulus remainder) (division
Return value Returns a node-set with all book and cd elements 10 2 24 2 true if price is 9.80 false if price is 9.90 true if price is 9.90 false if price is 9.80 true if price is 9.00 false if price is 9.80 true if price is 9.00 false if price is 9.90 true if price is 9.90 false if price is 9.80 true if price is 9.90 false if price is 9.70 or true if price is 9.80 false if price is 9.50 true if price is 9.80 false if price is 8.50 1
6+4 6-4 6*4 8 div 4 price=9.80 price!=9.80 price<9.80 price<=9.80 price>9.80 price>=9.80 price=9.80 price=9.70
XPath String functions: 1. substring(string,startindex,len) 2. substring-before(string1string2) Example: substring-before(abc,b) It returns a. 3. substring-after(string1string2) Example: substring-before(abc,b) It returns c.
Unit 12
4. contains(string1,string2): Returns Boolean. This returns true if string2 appears in string1, otherwise false. 5. startwith(string1,string2); Example: startwith(abc,ab) It returns true. 6. string-length(string) : It returns the length of a string. 7. normalize-space (string): Returns the input string after normalizing the white space, which includes eliminating white spaces in beginning and ending and remove multiple white space identified in between of the string, replacing with a single white space. Example: - - abc - - xyz In the above the dashes (-) are white spaces. Output: abc-xyz
12.14 Summary
Use XSLT to transform XML documents into other formats, like XHTML. In this chapter we learned how to add/remove elements and attributes to or from the output file. We also learned how to rearrange and sort elements, perform tests and make decisions about which elements to hide and display. This tutorial has taught you how to find information in an XML document. We learned how to use XPath to navigate through elements and attributes in an XML document. We also learned how to use some of the standard functions that are built-in in XPath.
Unit 12
Unit 12
References: