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="UTF-8"?>
<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 IE, Chrome, Firefox, Safari, and Opera: Open the XML file (click on the link below) - The XML document will be displayed with color-coded root and child elements (except in Safari). Often, there is a plus (+) or minus sign (-) to the left of the elements that can be clicked to expand or collapse the element structure. Tip: To view the raw XML source, right-click in XML file and select "View Source"!
Create an XSL Style Sheet
Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:
<?xml version="1.0" encoding="UTF-8"?>
<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>
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="UTF-8"?>
<?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>
If you have an XSLT compliant browser it will nicely transform your XML into XHTML.
Result:
My CD Collection
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |
Hide your heart | Bonnie Tyler |
Greatest Hits | Dolly Parton |
Still got the blues | Gary Moore |
Eros | Eros Ramazzotti |
One night only | Bee Gees |
Sylvias Mother | Dr.Hook |
Maggie May | Rod Stewart |
Romanza | Andrea Bocelli |
When a man loves a woman | Percy Sledge |
Black angel | Savage Rose |
1999 Grammy Nominees | Many |
For the good times | Kenny Rogers |
Big Willie style | Will Smith |
Tupelo Honey | Van Morrison |
Soulsville | Jorn Hoel |
The very best of | Cat Stevens |
Stop | Sam Brown |
Bridge of Spies | T`Pau |
Private Dancer | Tina Turner |
Midt om natten | Kim Larsen |
Pavarotti Gala Concert | Luciano Pavarotti |
The dock of the bay | Otis Redding |
Picture book | Simply Red |
Red | The Communards |
Unchain my heart | Joe Cocker |
The details of the example above will be explained in the next chapters.