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:
1<cfxml variable="xml1">
2 <myXml>
3 <cftest>my data</cftest>
4 </myXml>
5</cfxml>
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>
1<cfxml variable="xml2">
2<myXml>
3 <ctest>my data</ctest>
4</myXml>
5</cfxml>
2<myXml>
3 <ctest>my data</ctest>
4</myXml>
5</cfxml>
The afterwards get regex to make the changes.
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.
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.
@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.
Must be a difference between that and 7. Interesting.
<cfset cf = "cf" />
<cfxml variable="myXml">
<#cf#myTag>...</#cf#myTag>