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:

Posted: 16-Nov-2006

View: 5857

Permalink: here

Comments

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?

#1 Steven Erat
16/Nov/06 2:42 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.

#2 Danilo Celic
16/Nov/06 3:06 PM

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.

#3 Steve Bryant
16/Nov/06 3:15 PM

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.

#4 Mike Rankin
16/Nov/06 3:22 PM

Just a note: you might want to update the title of this post to "CFXML" instead of "CFMXL" for search and reference reasons. :-)

#5 Mike Kelp
16/Nov/06 4:24 PM

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

#6 Andy J
17/Nov/06 9:29 AM

Using cfoutput same result, it outputs something like: coldfusion.xml.XmlNodeList@4d95c7
Must be a difference between that and 7. Interesting.

#7 Danilo Celic
17/Nov/06 1:48 PM

More readable and less processing overhead would be:
<cfset cf = "cf" />
<cfxml variable="myXml">
<#cf#myTag>...</#cf#myTag>

#8 Scott Arbeitman
18/May/07 3:58 AM