CF_XSLT ColdFusion Custom Tag For Xml Transformations

Posted December 4, 2007 at 7:52 AM

Tags: ColdFusion

I was doing a bunch of stuff with XML and XSL Transformations in ColdFusion the other day and I got really tired of the fact that I had to create an intermediary XSLT variable before I could call the XmlTransform() method. To deal with this, I whipped up a quick little ColdFusion custom tag, xslt.cfm. This tag simply takes, as an attribute, an XML document (or string), and applies the XSL transformation defined within the tag contents to the given XML document. Here's how it works inside:

 Launch code in new window » Download code as text file »

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <!---
  • Check to see which mode of the tag we are executing.
  • In the Start mode, we want to get the attributes. In
  • the End mode, once we have the XSL transformation,
  • we want to perform the transformation.
  • --->
  • <cfif (THISTAG.ExecutionMode EQ "Start")>
  •  
  • <!--- Param Attributes. --->
  •  
  • <!---
  • This is the XML document that we are going to
  • transform. It can be either an XML document object
  • or an XML string.
  • --->
  • <cfparam
  • name="ATTRIBUTES.XML"
  • type="any"
  • />
  •  
  • <!--- Exit the start mode of the tag. --->
  • <cfexit method="exittemplate" />
  •  
  • <cfelse>
  •  
  • <!---
  • Reset the generated output to be the result of
  • the XML transformation.
  • --->
  • <cfset THISTAG.GeneratedContent = XmlTransform(
  • Trim( ATTRIBUTES.XML ),
  • Trim( THISTAG.GeneratedContent )
  • ) />
  •  
  • </cfif>
  •  
  • </cfsilent>

Notice that in the Start execution mode of the tag, all we do is define the XML attribute. Then, in the End execution mode of the tag we simply replace the generated content with the result of the XmlTranform() method using the passed in the XML document and the existing generated content value.

To use the tag, you could do something like this:

 Launch code in new window » Download code as text file »

  • <!--- Build the XML document. --->
  • <cfxml variable="xmlData">
  •  
  • <page>
  • <navigation>
  • <item on="yes">
  • <text>Home</text>
  • <href>home.cfm</href>
  • </item>
  • <item>
  • <text>About</text>
  • <href>about.cfm</href>
  • </item>
  • <item>
  • <text>Contact Us</text>
  • <href>contact.cfm</href>
  • </item>
  • </navigation>
  • </page>
  •  
  • </cfxml>
  •  
  •  
  • <!--- Transform the xml data. --->
  • <cf_xslt xml="#xmlData#">
  •  
  • <?xml version="1.0" encoding="ISO-8859-1"?>
  •  
  • <xsl:transform
  • version="1.0"
  • xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  •  
  •  
  • <!--- Match primary navigation. --->
  • <xsl:template match="page/navigation">
  •  
  • <ul class="primarynav">
  • <xsl:apply-templates select="item" />
  • </ul>
  •  
  • </xsl:template>
  •  
  •  
  • <!--- Match primary navigation items. --->
  • <xsl:template match="item">
  •  
  • <li>
  • <xsl:if test="@on = 'yes'">
  • <xsl:attribute name="class">
  • <xsl:text>on</xsl:text>
  • </xsl:attribute>
  • </xsl:if>
  •  
  • <a>
  • <xsl:attribute name="href">
  • <xsl:value-of select="href" />
  • </xsl:attribute>
  •  
  • <xsl:value-of select="text" />
  • </a>
  • </li>
  •  
  • </xsl:template>
  •  
  • </xsl:transform>
  •  
  • </cf_xslt>

Here I am modelling the navigation of an HTML page and then using the CF_XSLT ColdFusion custom tag to render it in place. This might seem like a really small piece of functionality, but it ended up making my life easier.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »