Click to search Andy Jarrett.co.uk RSS feed

Loading Twitter

CFXML and CF tags gotcha

As i'm preparing to type this post what I'm about to says is so obvious its annoying. When you are using the <cfxml> tag you cannot have xml tags that begin with "cf" as Coldfusion tries to parse them. For example:

view plain print about
1<cfxml variable="xml1">
2    <myXml>
3        <cftest>my data</cftest>
4    </myXml>
5</cfxml>

Would fail with something like "A tag starting with 'CF' has been detected. This tag is not supported by this version of ColdFusion. Please verify your typo and try again. Unknown tag: cftest. "

The workaround:

Modify the tag from <cftest> to <ctest>

view plain print about
1<cfxml variable="xml2">
2<myXml>
3    <ctest>my data</ctest>
4</myXml>
5</cfxml>

The afterwards get regex to make the changes.

view plain print about
1<cfset xml2 = reReplace(xml2, "<(.*?)c", "<\1cf" , "all") />
To explain the regex.
  • Attribute 1, is the string your are modifying.
  • Attribute 2, is the regex here. Here I am using creating a back reference (think of it as a variable) to catch the closing tag backslash.
  • Attribute 3, is the same as att. 2 but we remove the brackers and call the back reference via "\1".
  • Tells us to capturing all occurances.

Comments Comments (8) | Print Print | Send Send | 4747 Views

Assuming you're trying to do some kind of CFML code generation, can you dynamically generate the CFML contents of the XML doc through string concatenation and then output that at runtime in the CFXML block?
Danilo Celic's Gravatar Posted By Danilo Celic @ 11/16/06 3:06 PM
And if you use any other tag starting with a "c", such as caption, cite, code your code will break it, so probably better to go with a multicharacter replacement, than just the "c".

Which version of CF are you using for this experiment? On my 6.1 I have to work with, when I cfdump xml2 after the ReReplace, I get something like the following: coldfusion.xml.XmlNodeList@ac3205 rather than the "normal" structure dump that I get if I dump the variable prior to the ReReplace.
Depending on your situation, another option might be to write the XML to a file and then get the contents of the file from cffile and pass them into cfxml.
That just seems goofy. I think I would consider that a bug of some sort unless there is a proscribed method for dealing with the situation. I haven't tried this, but might a workaround be to use a namespace? I don't think the cfxml tag will care.
Just a note: you might want to update the title of this post to "CFXML" instead of "CFMXL" for search and reference reasons. :-)
@Mike Kelp, Good catch cheers for that!

@Mike Rankin, I thought it might be a bug as well but at the end of the day <cf should initiate a ColdFusion tag. If anything there should (and might be) a way to escape it.

@Danilo, Im running my code on 7.02. Have you tried just outputting the code in a CF output block, It might make a difference?

@Everyone else, I wasn't trying to do some CF generation, I was actually building an XML doc for a client which required a tag to begin with <cf. Steve, thanks for the idea. I've just created
cfset starttag = "<cf" />
cfset endtag = "</cf" />

And then used the variable in the CFXML block.
Danilo Celic's Gravatar Posted By Danilo Celic @ 11/17/06 1:48 PM
Using cfoutput same result, it outputs something like: coldfusion.xml.XmlNodeList@4d95c7
Must be a difference between that and 7. Interesting.
More readable and less processing overhead would be:
<cfset cf = "cf" />
<cfxml variable="myXml">
<#cf#myTag>...</#cf#myTag>
BlogCFC by Raymond Camden + Twitter @AndyJ + ColdFusion jobs + Contact Me + Snippets/Downloads + RSS .