Transforming XML into Content by Using XSLT
CFML is an excellent tool for transforming content from a relational database int228, XML, or almost any form of content markup due to its native support for looping over a query set. However, this ease of looping and data handling does not extend to XML. ColdFusion can certainly loop over data in an XML object, and can search for data using XmlSearch(), but there is a much better solution for transforming XML into other forms of content.XSLT (eXtensible Stylesheet Language for Transformations) was created by the W3C in 1998 as a way to easily define a transformation from an XML document to some other content format. Using XSLT, I could transform a document like Listing 15.1 into th218 table shown in Figure 15.2 using a simple stylesheet:
Figure 15.2. HTML table.

Creating a basic transformation
Before we can build a complex listing of artists, CDs, ratings, and recommendations, let's start out with a simple nested bulleted list of artists and CDs. The list will display each artist's name in bold, followed by a bulleted list of the artist's genres and a separate list of the artist's CDs. Listing 15.3 shows an XSL stylesheet that gives us this information.
Listing 15.3. BasicTransformation.xslAn XSLT stylesheet to create a simple listing of artists and CDs
Running that transformation (see "Listing 15.4.
<xsl:transform
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/cdcollection">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="/cdcollection/artist">
<li>
<b><xsl:value-of select="@name" /></b>
<br />
Genre:
<ul>
<xsl:apply-templates select="genre" />
</ul>
CDs:
<ul>
<xsl:apply-templates select="cd" />
</ul>
</li>
</xsl:template>
<xsl:template match="/cdcollection/artist/genre">
<li><xsl:value-of select="." /></li>
</xsl:template>
<xsl:template match="/cdcollection/artist/cd">
<li><xsl:value-of select="@name" /></li>
</xsl:template>
</xsl:transform>
Listing 15.4. ArtistsAndCDsA simple listing of artists and CDs
Don't be overwhelmed by the stylesheet. It may seem completely foreign, so let's break it down bit by bit.
<ul>
<li>
<b>Air</b><br/>
Genre:
<ul>
<li>Electronic</li>
</ul>
CDs:
<ul>
<li>10,000 Hz Legend</li>
<li>Talkie Walkie</li>
</ul>
</li>
<li>
<b>Kylie Minogue</b><br/>
Genre:
<ul>
<li>Dance</li>
</ul>
CDs:
<ul>
<li>Fever</li>
<li>Body Language</li>
</ul>
</li>
<li>
<b>Dannii Minogue</b><br/>
Genre:
<ul>
<li>Dance</li>
<li>Electronic</li>
</ul>
CDs:
<ul>
<li>Neon Nights</li>
<li>You Won't Forget About Me EP</li>
</ul>
</li>
<li>
<b>Brooklyn Funk Essentials</b><br/>
Genre:
<ul>
<li>Funk</li>
<li>Dance</li>
<li>Spoken Word</li>
</ul>
CDs:
<ul>
<li>Cool & Steady & Easy</li>
</ul>
</li>
<li>
<b>Felix Da Housecat</b><br/>
Genre:
<ul>
<li>Electronica</li>
<li>Dance</li>
<li>Retro</li>
</ul>
CDs:
<ul>
<li>A Bugged Out Mix</li>
<li>Kittenz and Thee Glitz</li>
<li>Devin Dazzle & The Neon Fever</li>
</ul>
</li>
</ul>
<xsl:transform>
XSL stylesheets are well-formed XML documents that use a specific set of tags to tell the XSL processor how to run the transformation. All transformation stylesheets use <xsl:transform> as their root element. (You may sometimes see <xsl:stylesheet> used instead of <xsl:transform>; they are interchangeable in most cases.)The version attribute of <xsl:transform> specifies the version of the XSL standard that will be used to process the stylesheet, and the xmlns:xsl attribute identifies the namespace that contains the XSL tags. By and large, the transformations you create will always use the same values for these attributes as you see in Listing 15.3.
<xsl:output>
The first tag inside <xsl:transform> in <xsl:output> were not present, the resultin220 file would have as its first line:
which is not valid in a227 document. There are nine other attributes of <xsl:output> that let you specify other behaviors of the output engine; these are summarized in , or text
<?xml version="1.0" encoding="UTF-8"?>