Skip to main content
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Michael Evangelista and Avery Shumway
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Michael Evangelista ( @m_evangelista ) Avery Shumway

Converting XML To HTML Using ColdFusion And XSLT

Published in Comments (4)

As part of my exploration of XML, I've been playing around more with ColdFusion and XSLT. For this experiment, I am trying to encapsulate the data of an HTML page in XML. Then, I am using ColdFusion and XSLT to transform that XML document into an XHTML page. The ColdFusion XML document that I am starting off with contains:

  • Page Title
  • Meta Data information
  • Navigation elements
  • Primary Content information

Here is my ColdFusion XML document, built using the CFXML tag:

	Define the ColdFusion XML document object that
	will represent the data on an HTML page.
<cfxml variable="xmlPage">

			Maria Bello Fan Site

		<!--- Meta information. --->
				maria bello,sexy girls,crazy sexy
				My Maria Bello fan site.

		<!--- Primary navigation. --->

			Primary page content. This will make up the bulk
			of the rendered page.
					Welcome to my Maria Bello Fan Site. If you
					are like me, then you think she's just all
					kinds of sexy. I've spent many hours
					compiling this repository of images and
					videos for your (and my) enjoyment. Hope you
					enjoy and please check back soon.

					If Maria Bello were a cereal, she'd be
					magically babelicious.


You will notice that I am keeping a lot of white space in my XML nodes (ex. keywords and description nodes). This is NOT recommended. This adds white space to the final XML node value. I am doing this purely for better code display, not as a best practice. Notice also that the primary content of the page is contained in a CDATA node. From what I have read about XSLT in general, it would appear that CDATA node type is not well liked (and perhaps not widely supported???); however, it seems to work quite nicely for XHTML type pages.

Then, once I have my ColdFusion XML document ready, I need to create my XSLT templates. These are the XML directives that will take the ColdFusion XML document and translate it in a divide-and-conquer fashion into the resultant data file which is, in our case, an XHTML page.

<!--- Define the XSL Transformation (XSLT). --->
<cfsavecontent variable="strXSLT">

	<!--- Document type declaration. --->
	<?xml version="1.0" encoding="ISO-8859-1"?>


			Tell the ColdFusion XSLT engine that we are
			going to be producing an HTML document and that we
			want to use indenting.

			Bind this template to the root of the XML document
			using the "/" match attribute.
		<xsl:template match="/">

					<!--- Output the page title. --->
					<xsl:value-of select="page/title" />

				<!--- Output meta data template. --->
				<xsl:apply-templates select="page/meta" />

					<xsl:value-of select="page/title" />

				<!--- Output primary navigation. --->
				<xsl:apply-templates select="//primarynav" />

				<!--- Output page content. --->



		<!--- Template for HTLM meta data. --->
		<xsl:template match="meta">

			<!--- Output met akeywords element. --->
			<xsl:element name="meta">
				<xsl:attribute name="keywords">
					<xsl:value-of select="keywords" />

			<!--- Output meta description element. --->
			<xsl:element name="meta">
				<xsl:attribute name="description">
					<xsl:value-of select="description" />


		<!--- Template for the primary navigation items. --->
		<xsl:template match="primarynav">

			<ul id="primarynav">
				<!--- Loop over nav items. --->
				<xsl:for-each select="navitem">

						<!--- Define the nav link HTML. --->
						<xsl:element name="a">
							<xsl:attribute name="href">
								<xsl:value-of select="@href" />

								Check to see if this link is on.
								If so, then we have to add the
								appropriate class.
							<xsl:if test="@ison = 'true'">
								<xsl:attribute name="class">

							<!--- Text of link. --->
							<xsl:value-of select="@text" />



		<!--- Template for primary page content. --->
		<xsl:template match="primarycontent">

			<div id="sitecontent">

					When outputting the page's primary content,
					make sure to DISABLE escaping so that our
					XHTML will come through properly.





	Output the transformed XML page data into an
	HTML formatted web page using XSLT.
	Trim( strXSLT )

I have chosen to make this ColdFusion XML Transformation a bit more complicated than it needs to be for the purposes of forcing myself to learn more about the XSLT syntax. Some of this complexity is self-imposed, but also, XSLT is just a complicated syntax. For instance, instead of just writing an XHTML element and using some conditional logic to write attributes (such as adding an "active" class to an "on" navigational item), you have to use the xsl:element node and add each attribute as a descendent xsl:attribute node. A bit wordy if you ask me.

Also, notice that when I am outputting the CDATA, I am using the xsl:value-of attribute, disable-output-escaping. I have to do this other wise the XSLT engine will convert the HTML elements into escaped HTML elements.

Running the above code, we get the following page:

Convertted XML To XHTML Using ColdFusion And XSLT

XSLT is interesting. I think I am a little turned off by how complicated it seems to be. On the one hand, I like that you can take XML and convert it to any other kind of text-based data based on the Transformation files. But on the other hand, I feel like you can use ColdFusion to do this programmatically with much less stress. Plus, I think white space management is a big issue in XSLT. I like being able to space my ColdFusion code out in a way that makes me happy and produce quality output. I think that with XSLT, this is going to be a much harder task to accomplish.

Still not sure how I feel about it. I am gonna keep poking around; XSLT is HUGE! There are many functions and elements to be used. Luckily, it looks like ColdFusion only supports a subset of this functionality, so it won't be the beast that it could be. Plus, I am not sure I need to fully learn the syntax to get an idea of what is capable. The good thing is that it keeps me fresh with the XPath stuff I did a while back.

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

Reader Comments



You maginificent bastard! That works (the normalize-space function). I had tried to use xsl:strip-space but couldn't get it to work. Maybe I wasn't doing it right it just wasn't supported in ColdFusion. Nicely done!


No problem!

The xsl:strip-space is to remove empty nodes from your XML so they don't display the whitespace of the empty node. The function name is a little misleading, I know for sure I was confused by it when I started using XSLT. From the link above:

"You can also automatically delete white-space only nodes in the input document by using xsl:strip-space."



Yes, I agree. We just got the Google Mini, which comes with built-in xslt, which you can edit. It just seemed so much easier to return the results in XML, convert them to a query, and then manipulate them through ColdFusion.

This post helped reaffirm my decision.

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