Skip to main content
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Alexander Friess and Gary Gilbert
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Alexander Friess ( @AlexanderFriess ) Gary Gilbert ( @garyrgilbert )

CF_XSLT ColdFusion Custom Tag For Xml Transformations

By on

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

		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.

		<!--- Exit the start mode of the tag. --->
		<cfexit method="exittemplate" />


			Reset the generated output to be the result of
			the XML transformation.
		<cfset THISTAG.GeneratedContent = XmlTransform(
			Trim( THISTAG.GeneratedContent )
			) />



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">

			<item on="yes">
				<text>Contact Us</text>


<!--- Transform the xml data. --->
<cf_xslt xml="#xmlData#">

	<?xml version="1.0" encoding="ISO-8859-1"?>


		<!--- Match primary navigation. --->
		<xsl:template match="page/navigation">

			<ul class="primarynav">
				<xsl:apply-templates select="item" />


		<!--- Match primary navigation items. --->
		<xsl:template match="item">

				<xsl:if test="@on = 'yes'">
					<xsl:attribute name="class">

					<xsl:attribute name="href">
						<xsl:value-of select="href" />

					<xsl:value-of select="text" />




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.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel