XSLT And XMLTransform() Work Properly With XHTML In ColdFusion

Posted January 12, 2009 at 9:19 AM

Tags: ColdFusion

I have done a good amount of experimentation with XSLT and the XMLTransform() method in ColdFusion; but, up until now, that's all pretty much been with strict XML documents. Over the weekend, I was thinking of re-architecting some parts of my blog and I thought XSLT might just be the best approach. But then it occurred to me that I didn't know if XSLT would play nicely with XHTML-style data. To be honest, I am not even sure if XHTML validates as XML. For the most part, the parallel nature of the two types of document are obvious; the one uncertainty for me is the mixture of text nodes and elements within a single parent.

To see how XSLT and XHTML work together, I set up a simple demo that creates a copy of a chunk of XHTML data by copying each node within it separately:

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

  • <!--- Define XHTML style data. --->
  • <cfsavecontent variable="strData">
  •  
  • <p class="content">
  • This is some text with some other formatted text
  • <strong>contained within in</strong>. While this is
  • valid XHTML, I am wondering how it will hold up
  • when put through <em>XSLT</em> node copying.
  • <img src="about:blank" /> Embedded image.
  • </p>
  •  
  • </cfsavecontent>
  •  
  •  
  • <!--- Define the XSLT. --->
  • <cfsavecontent variable="strXSLT">
  •  
  • <!--- Document type declaration. --->
  • <?xml version="1.0" encoding="ISO-8859-1"?>
  •  
  • <xsl:transform
  • version="1.0"
  • xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  •  
  • <!--- Match all generic nodes. --->
  • <xsl:template match="*">
  • <!--- Copy this node (non-deep copy). --->
  • <xsl:copy>
  • <!---
  • Make sure that all attributes are copied
  • over for the current node.
  • --->
  • <xsl:copy-of select="@*" />
  •  
  • <!---
  • Apply templates to all of it's child nodes
  • (so that they can be copied).
  • --->
  • <xsl:apply-templates />
  • </xsl:copy>
  • </xsl:template>
  •  
  • </xsl:transform>
  •  
  • </cfsavecontent>
  •  
  •  
  • <!---
  • Transfor the XHTML. Let's see if this creates an accurate
  • copy of the XHTML.
  • --->
  • #HtmlEditFormat(
  • XMLTransform(
  • Trim( strData ),
  • Trim( strXSLT )
  • )
  • )#

As you can see, my XSLT is simply copying each node and then recursively calling Copy on each of its child nodes. When we run this code, we get the following output:

<?xml version="1.0" encoding="UTF-8"?> <p class="content"> This is some text with some other formatted text <strong>contained within in</strong>. While this is valid XHTML, I am wondering how it will hold up when put through <em>XSLT</em> node copying. <img src="about:blank"/> Embedded image. </p>

With the exception of the added XML DocType, I am happy to see that ColdFusion's XMLTransform() and my XSLT work quite well with XHTML style data. This verifies that XHTML does indeed validate as XML. Let's see if I can start putting this new found compatibility insight into use.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




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

Reader Comments

Jan 12, 2009 at 11:46 AM // reply »
7 Comments

One thing to keep in mind is that CF8 still ships with the old Xalan processor from Apache. Unfortunately, that parser only supports xslt 1.0 and hasn't had any active development in years. What Adobe really needs to do is upgrade that library to the Saxon-B parser so that it supports XSLT 2.0, XQuery 1.0, and XPath 2.0.

There is a way to swap out the parser for the updated one, but then you have to remember to do it each time you update CF.


Jan 12, 2009 at 11:49 AM // reply »
6,516 Comments

@Mike,

This is a good point. However, for most of what I will am thinking about doing, this should be good enough for now. I'll have some more blog posts on the related topics to come.


Jan 12, 2009 at 6:19 PM // reply »
10 Comments

"To be honest, I am not even sure if XHTML validates as XML" - XHTML was developed to represent HTML as valid XML - http://www.w3.org/TR/xhtml1/#xhtml


Jan 13, 2009 at 12:30 PM // reply »
6,516 Comments

@Johans,

Awesome. Good to know. And, since I use XStandard as my blog editor, I can rest assured that all of my blog content is actually XHTML / XML compliant. Man, I love XStandard.

http://www.bennadel.com/blog/tags/15-XStandard-WYSIWYG-blog-entries.htm


Jan 13, 2009 at 1:34 PM // reply »
10 Comments

Agree 100% - XStandard is the way to go. It is built into my CMS:

http://www.assetnow.com

It would be great if they developed an ActionScript version for Flex/Air.


Jan 13, 2009 at 1:38 PM // reply »
6,516 Comments

@Johans,

Word up! XStandard is such an awesome editor! I hate when I have to use anything else.


Jan 13, 2009 at 2:29 PM // reply »
10 Comments

BTW - on topic of XS editor - I noticed in your XS posts you were concerned your web services would not work with v2 and was pleasantly surprised when they did.

The changes to the web services were minor and are listed in the XS OEM docs change log. Mainly added ability to order items returned in response and syntax for max upload size parameters.

The main change was the license file/format.


Jan 13, 2009 at 2:55 PM // reply »
6,516 Comments

@Johans,

Yeah, that was pretty exciting :)


Jan 13, 2009 at 5:44 PM // reply »
27 Comments

Too bad XStandard is NOT written in JS, but a browser plug-in.


Jan 13, 2009 at 5:46 PM // reply »
6,516 Comments

@Henry,

Sometimes ultimate power comes with minor compromises :)


Aug 5, 2009 at 4:47 PM // reply »
1 Comments

@Mike Rankin:
Thanks! Your comment spared me a lot of head-banging when I was trying to get some "xsl:character-map" stuff to work in CF 8.

Anyone know what parser CF 9 uses? (yes, I know I could install the beta and find out...)


Aug 5, 2009 at 7:05 PM // reply »
6,516 Comments

@Evan,

I'm not sure what the parse is these days. I assume it is the same one - I haven't seen any XML-based updates.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »