XSLT- Part 2

Author: Jaidev

 

 

Applying Templates

 

One can control the context in which a template is applied using the xsl:apply-template rule, the syntax of which is:

 

<xsl:apply-templates select="xpath-address"/>

 

where xpath-address is the XPath location of the elements to which the template must be applied.

 

Consider the XSLT below. Also, note that we are using the sample XML file we have created in the last chapter to provide us with the data. If you have not ready the last lesson, I strongly recommend you do so.

 

Text Box: <?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>List of Articles</h2> 
<xsl:apply-templates/> 
</body>
</html>
</xsl:template>
<xsl:template match="ARTICLEDATA">
<p>
<xsl:apply-templates select="TITLE"/> 
<xsl:apply-templates select="AUTHOR"/>
</p>
</xsl:template>
 
<xsl:template match="TITLE">
<b>TITLE: </b> <xsl:value-of select="."/> by 
</xsl:template> 
 
<xsl:template match="AUTHOR">
<b> AUTHOR: </b><xsl:value-of select="."/>
<br />
</xsl:template>
 
</xsl:stylesheet>
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Here the templates (marked in light blue small font) have been applied to the "TITLE" and "AUTHOR" elements within the "ARTICLEDATA" element producing the output shown below. In passing, note that the “select” attribute of xsl:apply-templates itself is optional and if omitted, a template will be applied to all the descendant elements.

Text Box: List of Articles
TITLE: XSLT Basics by AUTHOR: Jaidev
TITLE: CSharp Basics by AUTHOR: Aleksey N
TITLE: XML Revisited by AUTHOR: Visitor X M L
 

 

 

 

 

 

Sorting in XSLTs

 

Elements of an XML file can be sorted before they are sent for processing by an xsl:apply-templates or xsl:for-each directive. Consider the following XSLT.

 

Text Box: <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
       <xsl:template match="/">
             <html>
                    <body>
                           <h2>List of books</h2>
                           <table border="2">
                                 <tr>
                                        <b>
                                               <th>Title</th>
                                               <th>Author</th>
                                               <th>Abstract</th>
                                        </b>
                                 </tr>                                 
                                 <xsl:for-each select="ARTICLES/ARTICLE/ARTICLEDATA">
                                 <xsl:sort select="AUTHOR"/>
                                        <tr>
                                               <td><xsl:value-of select="TITLE"/></td>
                                               <td><xsl:value-of select="AUTHOR"/></td>
                                               <td><xsl:value-of select="ABSTRACT"/></td>
                                        </tr>
                                 </xsl:for-each>
                           </table>
                    </body>
             </html>
       </xsl:template>
</xsl:stylesheet>
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And the generated HTML output.

List of books

Title

Author

Abstract

CSharp Basics

Aleksey N

See its sharp!!

XSLT Basics

Jaidev

An XSLent document

XML Revisited

Visitor X M L

The XML text for you

 

Note that the rows of the table are sorted in ascending order of the “AUTHOR”. The syntax of the element performing the sort is:

 

<xsl:sort select="xpath-address"/>

 

where xpath-address is the address of the element on which the sort must be performed.

 

There are two optional attributes to this element. The first describes the sort order and the second specifies the type of data being sorted:

 

order=”descending”    [The default being “ascending”]

data-type=”number”   [The default being “text”]

 

Conditional Processing

 

The elements of an XML file can also be processed conditionally. There are basically two ways to do so:

 

¨      xsl:if   -  Used for writing if statements with only one condition. That is there is not even an else condition.

 

<xsl:if test=”some-test”>

 

where some-test is a test expression such as “AUTHOR = &quot;Jaidev&quot;

 

¨      xsl:choose – Used for writing if statements with more than one conditions.

 

<xsl:choose>

<xsl:when test=”some-test”>

<xsl:otherwise>

 

where some-test is a test expression such as “. = 2004” which implies the current element value must be equal to 2004.

 

We will see examples of both in action. First let us take the “xsl:if” example with the following XSLT.

 

Text Box: <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">.
       <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
       <xsl:template match="/">
             <html>
                    <body>
                           <h2>List of books</h2>
                           <table border="2">
                                 <tr><b>
                                        <th>Title</th>
                                        <th>Author</th>
                                        <th>Abstract</th>
                                 </b></tr>                                
                                 <xsl:for-each select="ARTICLES/ARTICLE/ARTICLEDATA">
                                        <tr>
                                        <td><xsl:value-of select="TITLE"/></td>
                                        <td><xsl:value-of select="AUTHOR"/>
                                               <xsl:if test="./AUTHOR = &quot;Jaidev&quot;">
                                   <b> (Editor)</b>
                                               </xsl:if></td>
                                        <td><xsl:value-of select="ABSTRACT"/></td>
                                        </tr>
                                 </xsl:for-each>
                           </table>
                    </body>
             </html>
       </xsl:template>
</xsl:stylesheet>
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The output HTML is shown below. Note that the string “(Editor)” in boldface has been added to the (conditional) name Jaidev. Note also that, while we can make one conditional action, we cannot provide an alternative action (for instance we cannot state what to do when the name is notJaidev”). This is done with the xsl:choose directive described below.

List of books

Title

Author

Abstract

XSLT Basics

Jaidev (Editor)

An XSLent document

CSharp Basics

Aleksey N

See its sharp!!

XML Revisited

Visitor X M L

The XML text for you

 

 

Now consider a modified version using the xsl:choose, xsl:when and xsl:otherwise directives. The idea here is to make Jaidev the “Editor”, Aleksey N as “Advisor” and all others as “Contributor”. As we have seen, this cannot be done with the simple xsl:if directive.

 

 

Text Box: <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
       <xsl:template match="/">
             <html>
                    <body>
                           <h2>List of books</h2>
                           <table border="2">
                                 <tr>
                                        <b>
                                               <th>Title</th>
                                               <th>Author</th>
                                               <th>Abstract</th>
                                        </b>
                                 </tr>                                 
                                 <xsl:for-each select="ARTICLES/ARTICLE/ARTICLEDATA">
                                        <tr>
                                        <td><xsl:value-of select="TITLE"/></td>
                                        <td>
                                   <xsl:choose>
                                   <xsl:when test="./AUTHOR = &quot;Jaidev&quot;">
                                             <xsl:value-of select="AUTHOR"/><b> (Editor)</b>
                                        </xsl:when>
                                   <xsl:when test="./AUTHOR = &quot;Aleksey N&quot;">
                                             <xsl:value-of select="AUTHOR"/><b> (Advisor)</b>
                                        </xsl:when>
                                             <xsl:otherwise>
                                             <xsl:value-of select="AUTHOR"/><b> (Contributor)</b>
                                        </xsl:otherwise>
                                     </xsl:choose>
                                               </td>
                                               <td><xsl:value-of select="ABSTRACT"/></td>
                                        </tr>
                                 </xsl:for-each>
                           </table>
                    </body>
             </html>
       </xsl:template>
</xsl:stylesheet>
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The output of the XSL Transform is shown below.

List of books

Title

Author

Abstract

XSLT Basics

Jaidev (Editor)

An XSLent document

CSharp Basics

Aleksey N (Advisor)

See its sharp!!

XML Revisited

Visitor X M L (Contributor)

The XML text for you

 

Note that:

 

¨      “xsl:choose” provides the overall scope of the conditional statement. Within this fall the xsl:when and xsl:otherwise directives.

¨      “xsl:when” provides the “if” test. There can be multiple xsl:when statements within a xsl:choose directive

¨      “xsl:otherwise” provides a “default” for all the cases that are not considered by the xsl:when directives.

 

Concluding XSLT Theory!

 

With those directives we conclude the lesson on XSLT. While you have gone through the main XSLT elements, these are by no means an exhaustive list. For that, please use the reference at the XSLT source:

http://www.w3.org/TR/xslt

 

XSLTs are a very useful powerful tool to transform XML data. They are often used to repurpose content for multiple devices and to different formats. In the next chapter, we will look at some interesting examples that demonstrate these capabilities.

Copyright© 2009 C# Computing, LLC