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.