XSLT Examples

Input document for examples is the same as one used in XPath examples!
1. The simplest possible stylesheet
Every XSL stylesheet must start with xsl:stylesheet element. The atribute version='1.0' specifies version of XSL(T) specification. This example shows the simplest possible stylesheet.
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
</xsl:stylesheet>
2. xsl:template element
Each <xsl:template> element contains rules to apply when a specified node is matched.
The match attribute is used to associate the template with an XML element.
match="/" defines the whole document
XSLT stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Example</h2>
<br/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Browser view
Example
Text view
<html>
<body>
<h2>Example</h2><br></body>
</html>
3. xsl:value-of element
With the following XSLT we will take just contents of the firstname and the lastname elements
XSLT stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<h1>
<xsl:value-of select="//firstname"/>
</h1>
<h2>
<xsl:value-of select="//lastname"/>
</h2>
</xsl:template>
</xsl:stylesheet>
Text view:
<h1>Joe</h1>
<h2>User</h2>
Browser view:
Joe
User
5. xsl:for-each element – getting product description and productID(s) in a table
The <xsl:for-each> element allows you to do looping in XSLT
XSLT stylesheet
<?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 products</h2>
<table border="1">
<tr>
<th>Product description</th>
<th>Product name</th>
</tr>
<xsl:for-each select="Data/instanceData/POdocument/items/item">
<tr>
<td>
<xsl:value-of select="string(productID/@description)"/>
</td>
<td>
<xsl:value-of select="productID/text()"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Text view:
<html>
<body>
<h2>List of products</h2>
<table border="1">
<tr>
<th>Product description</th>
<th>Product name</th>
</tr>
<tr>
<td>Product_1</td>
<td>CO11</td>
</tr>
<tr>
<td>Product_2</td>
<td>CO12</td>
</tr>
<tr>
<td>Product_3</td>
<td>CO13</td>
</tr>
</table>
</body>
</html>
Browser view:
List of products
Product description
|
Product name
|
Product_1
|
CO11
|
Product_2
|
CO12
|
Product_3
|
CO13
|
6. xsl:for-each element – getting all the product details in a table
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>List of products</h2>
<table border="1">
<tr>
<th>Product description</th>
<th>Product name</th>
<th>Unit price</th>
<th>Quantity</th>
</tr>
<xsl:for-each select="Data/instanceData/POdocument/items/item">
<tr>
<td>
<xsl:value-of select="string(productID/@description)"/>
</td>
<td>
<xsl:value-of select="productID/text()"/>
</td>
<td>
<xsl:value-of select="price/text()"/>
</td>
<td>
<xsl:value-of select="quantity/text()"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Text view:
<html>
<body>
<h2>List of products</h2>
<table border="1">
<tr>
<th>Product description</th>
<th>Product name</th>
<th>Unit price</th>
<th>Quantity</th>
</tr>
<tr>
<td>Product_1</td>
<td>CO11</td>
<td>1.23</td>
<td>5</td>
</tr>
<tr>
<td>Product_2</td>
<td>CO12</td>
<td>2.34</td>
<td>7</td>
</tr>
<tr>
<td>Product_3</td>
<td>CO13</td>
<td>3.45</td>
<td>9</td>
</tr>
</table>
</body>
</html>
Browser view:
List of products
Product description
|
Product name
|
Unit price
|
Quantity
|
Product_1
|
CO11
|
1.23
|
5
|
Product_2
|
CO12
|
2.34
|
7
|
Product_3
|
CO13
|
3.45
|
9
|
7. xsl:sort element – sorting by price in desc order
The <xsl:sort> element is used to sort the output.
XSLT stylesheet
<?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 products</h2>
<table border="1">
<tr>
<th>Product description</th>
<th>Product name</th>
<th>Unit price</th>
<th>Quantity</th>
</tr>
<xsl:for-each select="Data/instanceData/POdocument/items/item">
<xsl:sort order="descending"/>
<tr>
<td>
<xsl:value-of select="string(productID/@description)"/>
</td>
<td>
<xsl:value-of select="productID/text()"/>
</td>
<td>
<xsl:value-of select="price/text()"/>
</td>
<td>
<xsl:value-of select="quantity/text()"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Text view:
<html>
<body>
<h2>List of products</h2>
<table border="1">
<tr>
<th>Product description</th>
<th>Product name</th>
<th>Unit price</th>
<th>Quantity</th>
</tr>
<tr>
<td>Product_3</td>
<td>CO13</td>
<td>3.45</td>
<td>9</td>
</tr>
<tr>
<td>Product_2</td>
<td>CO12</td>
<td>2.34</td>
<td>7</td>
</tr>
<tr>
<td>Product_1</td>
<td>CO11</td>
<td>1.23</td>
<td>5</td>
</tr>
</table>
</body>
</html>
Browser view:
List of products
Product description
|
Product name
|
Unit price
|
Quantity
|
Product_3
|
CO13
|
3.45
|
9
|
Product_2
|
CO12
|
2.34
|
7
|
Product_1
|
CO11
|
1.23
|
5
|