Fro on Using ColdFusion to transform XML with XSLT
Working with XSLT to transform XML documents for a new project.
For this example I'll use the RSS 2.0 format. First we have to create the XML that we're going to transform. You could read in an XML file using any normal method (cffile, cfhttp, etc.). I'll just create my own using cfxml. I used the RSS 2.0 example from Wikipedia.
<rss version="2.0">
<channel>
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Weblog Editor 2.0</generator>
<managingEditor>editor@example.com</managingEditor>
<webMaster>webmaster@example.com</webMaster>
<item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's Star City.</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
</item>
<item>
<title>Space Exploration</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a partial eclipse of the Sun on Saturday, May 31st.</description>
<pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
</item>
<item>
<title>The Engine That Does More</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
<description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description>
<pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
</item>
<item>
<title>Astronauts' Dirty Laundry</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
<description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.</description>
<pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
</item>
</channel>
</rss>
</cfxml>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xhtml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/rss/channel">
<xsl:element name="h1">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
</xsl:element>
<xsl:for-each select="item">
<xsl:element name="h2">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
</xsl:element>
<xsl:element name="p">
<xsl:value-of select="description" />
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
</cfsavecontent>
<cfoutput>#myOutput#</cfoutput>
I want to end this by saying that you don't need CF to use XSL transformations. You can use XSL stylesheets directly in your XML documents. Check out some of these resources.
http://www.w3schools.com/xml/xml_xsl.asp
http://www.xmlfiles.com/xsl/
http://www.w3.org/Style/XSL/
