Tuesday, December 15, 2009

XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.
XSLT stands for XSL Transformations. In this tutorial you will learn how to use XSLT to transform XML documents into other formats, like XHTML.

XML Code

?xml version="1.0" encoding="ISO-8859-1"?>
!-- Edited by XMLSpy® -->
catalog>
    cd>
        title>Empire Burlesque/title>
        artist>Bob Dylan/artist>
        country>USA/country>
        company>Columbia/company>
        price>10.90/price>
        year>1985/year>
    /cd>
    cd>
        title>Hide your heart/title>
        artist>Bonnie Tyler/artist>
        country>UK/country>
        company>CBS Records/company>
        price>9.90/price>
        year>1988/year>
    /cd>
    cd>
        title>Greatest Hits/title>
        artist>Dolly Parton/artist>
        country>USA/country>
        company>RCA/company>
        price>9.90/price>
        year>1982/year>
    /cd>
    cd>
        title>Still got the blues/title>
        artist>Gary Moore/artist>
        country>UK/country>
        company>Virgin records/company>
        price>10.20/price>
        year>1990/year>
    /cd>
    cd>
        title>Eros/title>
        artist>Eros Ramazzotti/artist>
        country>EU/country>
        company>BMG/company>
        price>9.90/price>
        year>1997/year>
    /cd>
    cd>
        title>One night only/title>
        artist>Bee Gees/artist>
        country>UK/country>
        company>Polydor/company>
        price>10.90/price>
        year>1998/year>
    /cd>
    cd>
        title>Sylvias Mother/title>
        artist>Dr.Hook/artist>
        country>UK/country>
        company>CBS/company>
        price>8.10/price>
        year>1973/year>
    /cd>
    cd>
        title>Maggie May/title>
        artist>Rod Stewart/artist>
        country>UK/country>
        company>Pickwick/company>
        price>8.50/price>
        year>1990/year>
    /cd>
    cd>
        title>Romanza/title>
        artist>Andrea Bocelli/artist>
        country>EU/country>
        company>Polydor/company>
        price>10.80/price>
        year>1996/year>
    /cd>
    cd>
        title>When a man loves a woman/title>
        artist>Percy Sledge/artist>
        country>USA/country>
        company>Atlantic/company>
        price>8.70/price>
        year>1987/year>
    /cd>
    cd>
        title>Black angel/title>
        artist>Savage Rose/artist>
        country>EU/country>
        company>Mega/company>
        price>10.90/price>
        year>1995/year>
    /cd>
    cd>
        title>1999 Grammy Nominees/title>
        artist>Many/artist>
        country>USA/country>
        company>Grammy/company>
        price>10.20/price>
        year>1999/year>
    /cd>
    cd>
        title>For the good times/title>
        artist>Kenny Rogers/artist>
        country>UK/country>
        company>Mucik Master/company>
        price>8.70/price>
        year>1995/year>
    /cd>
    cd>
        title>Big Willie style/title>
        artist>Will Smith/artist>
        country>USA/country>
        company>Columbia/company>
        price>9.90/price>
        year>1997/year>
    /cd>
    cd>
        title>Tupelo Honey/title>
        artist>Van Morrison/artist>
        country>UK/country>
        company>Polydor/company>
        price>8.20/price>
        year>1971/year>
    /cd>
    cd>
        title>Soulsville/title>
        artist>Jorn Hoel/artist>
        country>Norway/country>
        company>WEA/company>
        price>7.90/price>
        year>1996/year>
    /cd>
    cd>
        title>The very best of/title>
        artist>Cat Stevens/artist>
        country>UK/country>
        company>Island/company>
        price>8.90/price>
        year>1990/year>
    /cd>
    cd>
        title>Stop/title>
        artist>Sam Brown/artist>
        country>UK/country>
        company>A and M/company>
        price>8.90/price>
        year>1988/year>
    /cd>
    cd>
        title>Bridge of Spies/title>
        artist>T`Pau/artist>
        country>UK/country>
        company>Siren/company>
        price>7.90/price>
        year>1987/year>
    /cd>
    cd>
        title>Private Dancer/title>
        artist>Tina Turner/artist>
        country>UK/country>
        company>Capitol/company>
        price>8.90/price>
        year>1983/year>
    /cd>
    cd>
        title>Midt om natten/title>
        artist>Kim Larsen/artist>
        country>EU/country>
        company>Medley/company>
        price>7.80/price>
        year>1983/year>
    /cd>
    cd>
        title>Pavarotti Gala Concert/title>
        artist>Luciano Pavarotti/artist>
        country>UK/country>
        company>DECCA/company>
        price>9.90/price>
        year>1991/year>
    /cd>
    cd>
        title>The dock of the bay/title>
        artist>Otis Redding/artist>
        country>USA/country>
        company>Atlantic/company>
        price>7.90/price>
        year>1987/year>
    /cd>
    cd>
        title>Picture book/title>
        artist>Simply Red/artist>
        country>EU/country>
        company>Elektra/company>
        price>7.20/price>
        year>1985/year>
    /cd>
    cd>
        title>Red/title>
        artist>The Communards/artist>
        country>UK/country>
        company>London/company>
        price>7.80/price>
        year>1987/year>
    /cd>
    cd>
        title>Unchain my heart/title>
        artist>Joe Cocker/artist>
        country>USA/country>
        company>EMI/company>
        price>8.20/price>
        year>1987/year>
    /cd>
/catalog>
XSLT Code






?xml version="1.0" encoding="ISO-8859-1"?>
!-- Edited by XMLSpy® -->
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

xsl:template match="/">
  html>
  body>
  h2>My CD 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>


XSLT Introduction

XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.
XPath is a language for navigating in XML documents.

What You Should Already Know

Before you continue you should have a basic understanding of the following:
  • HTML / XHTML
  • XML / XML Namespaces
  • XPath
If you want to study these subjects first, find the tutorials on our Home page.

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 (X)HTML element.
With XSLT you can add/remove elements and 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.
If you want to study XPath first, please read our XPath Tutorial.

How Does it Work?

In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.

XSLT - Transformation


Example study: How to transform XML into XHTML using XSLT.
The details of this example will be explained in the next chapter.

Correct Style Sheet Declaration

The root element that declares the document to be an XSL style sheet is xsl:stylesheet> or xsl:transform>.

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>Empire Burlesque/title>
    artist>Bob Dylan/artist>
    country>USA/country>
    company>Columbia/company>
    price>10.90/price>
    year>1985/year>
  /cd>
.
.
/catalog>

Viewing XML Files in Firefox 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.

View "cdcatalog.xml"
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>
  h2>My CD 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>

View "cdcatalog.xsl"
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>Empire Burlesque/title>
    artist>Bob Dylan/artist>
    country>USA/country>
    company>Columbia/company>
    price>10.90/price>
    year>1985/year>
  /cd>
.
.
/catalog>

 XSLT xsl:template> Element


An XSL style sheet consists of one or more set of rules that are called templates.
A template contains rules to apply when a specified node is matched.

 The xsl:template> Element

The xsl:template> element is used to build templates.

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

Ok, let's look at a simplified version of the XSL file from the previous chapter:
Example
?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 CD 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>

Example Explained

Since an XSL style sheet is an XML document, 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.

The last two lines define the end of the template and the end of the style sheet.

The result from this example was a little disappointing, because no data was copied from the XML document to the output. In the next chapter you will learn how to use the xsl:value-of> element to select values from the XML elements.

XSLT xsl:value-of> Element






The xsl:value-of> element is used to extract the value of a selected node.

The xsl:value-of> Element

The xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation:
Example
?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 CD Collection/h2>
  table border="1">
    tr bgcolor="#9acd32">
      th>Title/th>
      th>Artist/th>
    /tr>
    tr>
      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> 


Example Explained

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:
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 chapter you will learn how to use the xsl:for-each> element to loop through the XML elements, and display all of the records.

XSLT xsl:for-each> Element

The xsl:for-each> element allows you to do looping in XSLT.

The xsl:for-each> Element

The XSL xsl:for-each> element can be used to select every XML element of a specified node-set:

 ?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 CD 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>
Filtering the Output

We can also filter the output from the XML file by adding a criterion to the select attribute in the xsl:for-each> element.

xsl:for-each select="catalog/cd[artist='Bob Dylan']">

Legal filter operators are:

    * =  (equal)
    * != (not equal)
    * < less than
    * > greater than

Take a look at the adjusted XSL style sheet:
Example
?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 CD 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>
  /body>
  /html>
/xsl:template>

/xsl:stylesheet>

XSLT xsl:sort> Element

Where to put the Sort Information

To sort the output, simply add an xsl:sort> element inside the xsl:for-each> element in the XSL file:
Example
?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 CD Collection/h2>
  table border="1">
    tr bgcolor="#9acd32">
      th>Title/th>
      th>Artist/th>
    /tr>
    xsl:for-each select="catalog/cd">
      xsl:sort select="artist"/>
      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>



XSLT xsl:if> Element







The xsl:if> Element

To put a conditional if test against the content of the XML file, add an xsl:if> element to the XSL document.
Syntax
xsl:if test="expression">
  ...some output if the expression is true...
/xsl:if>

Where to Put the xsl:if> Element

To add a conditional test, add the xsl:if> element inside the xsl:for-each> element in the XSL file:
Example
?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 CD 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>


XSLT xsl:choose> Element

The xsl:choose> element is used in conjunction with xsl:when> and xsl:otherwise> to express multiple conditional tests.
The xsl:choose> Element
Syntax
xsl:choose>
  xsl:when test="expression">
    ... some output ...
  /xsl:when>
  xsl:otherwise>
    ... some output ....
  /xsl:otherwise>
/xsl:choose>

Where to put the Choose Condition

To insert a multiple conditional test against the XML file, add the xsl:choose>, xsl:when>, and xsl:otherwise> elements to the XSL file:
Example
?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 CD 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:otherwise>
          td>xsl:value-of select="artist"/>/td>
        /xsl:otherwise>
      /xsl:choose>
    /tr>
    /xsl:for-each>
  /table>
  /body>
  /html>
/xsl:template>

/xsl:stylesheet>

XSLT xsl:apply-templates> Element

The xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.
The xsl:apply-templates> Element

The xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.

If we add a select attribute to the xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.

Look at the following XSL style sheet:
Example
?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 CD Collection/h2>
  xsl:apply-templates/>
  /body>
  /html>
/xsl:template>

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>