XSLT examples

XML file for these examples


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="bookstore1.xsl"?>
<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

XPath example


<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Fun with XPath! -->
<!-- XPath is used to select elements -->
<xsl:template match="/">
  <html>
  <body>
  <h1>Book catalog</h1>
	<ul>
	<xsl:for-each select="bookstore/book">
	<!-- <xsl:for-each select="bookstore/book[price > 30.00]"> -->
	<!-- <xsl:for-each select="bookstore/book[@category = 'CHILDREN']"> -->
	<!-- <xsl:for-each select="bookstore/book[@category = 'CHILDREN' or price > 30.00]"> -->
	<!-- Note: IE starts at 0, not at 1: -->
	<!-- <xsl:for-each select="bookstore/book[1]"> --> 
	<!-- <xsl:for-each select="bookstore/book[last()]"> -->
	<!-- <xsl:for-each select="bookstore/book[position() mod 2 = 0]"> -->
	<!-- absolute path, matches any book element: -->
	<!-- <xsl:for-each select="//book[price > 30.00]"> -->
	<!-- <xsl:for-each select="bookstore/*"> --> <!-- wildcard -->
	<!-- combining paths using | -->
	<!-- <xsl:for-each select="//*[@category = 'CHILDREN'] | //*[price > 30.00]"> -->
	<li>
	<xsl:value-of select="title"/> by <xsl:value-of select="author"/>
	</li>
	</xsl:for-each>
	</ul>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
http://rynite.morris.umn.edu/~elenam/4657_spring07/xslt/bookstore1.xml

Formatting with XSLT


<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Formatting using XSLT -->
<xsl:template match="/">
  <html>
  <body>
  <h1>Book catalog</h1>
	<table style="border-collapse: collapse">
	<xsl:for-each select="bookstore/book">
	<!-- attributes of 'sort' allow you to fine-tune the criteria -->
	<!-- data-type="number" is needed to avoid lexicographic 
	sorting of numbers (10 < 9) -->
	<xsl:sort select="year" data-type="number" order="descending"/> 
	<tr>
	<td style="border: solid 2px"><xsl:value-of select="title"/></td> 
	<!-- TASK: display all authors, not just the first one -->
	<td style="border: solid 2px"><xsl:value-of select="author"/></td>
	<xsl:choose>
		<xsl:when test="price &gt; 40.00">
			<td style="border: solid 2px; background-color:pink">
			<xsl:value-of select="price"/></td>
		</xsl:when>
		<xsl:when test="price &gt; 30.00">
			<td style="border: solid 2px; background-color:yellow">
			<xsl:value-of select="price"/></td>		
		</xsl:when>
		<xsl:otherwise>
			<td style="border: solid 2px; background-color:lightgreen">
			<xsl:value-of select="price"/></td>	
		</xsl:otherwise>
	</xsl:choose>
	<td style="border: solid 2px"><xsl:value-of select="year"/></td>
	</tr>
	</xsl:for-each>
	</table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
http://rynite.morris.umn.edu/~elenam/4657_spring07/xslt/bookstore2.xml

Intro to XSLT functions and variables


<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="tax">0.05</xsl:variable>

<!-- Formatting using XSLT -->
<xsl:template match="/">
  <html>
  <body>
  <h1>Book catalog</h1>
	<table style="border-collapse: collapse">
	<xsl:for-each select="bookstore/book">
	<tr>
	<td style="border: solid 2px"><xsl:value-of select="title"/></td> 
	<td style="border: solid 2px"><xsl:value-of select="author"/></td>
	<td style="border: solid 2px"><xsl:apply-templates select="price"/></td>
	<td style="border: solid 2px"><xsl:value-of select="year"/></td>
	</tr>
	</xsl:for-each>
	</table>
  </body>
  </html>
</xsl:template>

<!-- compute sales tax for price using a function -->
<xsl:template match="price">
<xsl:value-of select="."/>, with tax: 
<xsl:value-of select=". * (1 + $tax)"/>
</xsl:template>
</xsl:stylesheet>
http://rynite.morris.umn.edu/~elenam/4657_spring07/xslt/bookstore3.xml