CF_XSLT ColdFusion Custom Tag For Xml Transformations

Posted December 4, 2007 at 7:52 AM by Ben Nadel

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:

  • <!--- 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:

  • <!--- 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.



Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »