For several versions of ColdFusion, we have been able to reference groups of XML nodes using pseudo node wrappers and struct and array notation. This has been a useful shorthand when extracting node data from an XML document; but, about two years ago, I found out that these pseudo node wrappers could not be used in conjunction with ColdFusion 8's (then) new CFLoop array iteration feature. In the comments to that post, however, Brad Roberts pointed out to me that this bug has been fixed in ColdFusion 9.
To test Brad's comment, I set up this quick little demo:
- <!--- Create XML variable. --->
- <cfxml variable="girls">
- <girl name="Tricia" />
- <girl name="Joanna" />
- <girl name="Kim" />
- <!--- Loop over pseudo array xml node wrapper. --->
- <!--- Output girl's name. --->
- #girl.xmlAttributes.name#<br />
As you can see, I am creating the XML document and then referring to the collection of "girl" nodes using the pseudo node wrapper:
I am then taking this pseudo node wrapper and iterating over it using the CFLoop tag. In ColdFusion 8, this would have thrown the following error:
Element XMLATTRIBUTES.NAME is undefined in GIRL.
However, in ColdFusion 9, this now works, and running the code above gives us the following output:
Very cool! This is just another one of the small, incremental upgrades available in ColdFusion 9 that is going to make our lives easier. The pseudo XML node wrappers already helped; but, being able to iterate over them using CFLoop's array iteration feature is really going to maximize that efficiency.
Thanks to Brad Roberts for pointing that out!
Looking For A New Job?
- Advanced ColdFusion Developer at HD Web Studio
- ColdFusion Developer / Programmer at Blue Tangerine Solutions
- ColdFusion Web Developer (Junior or Mid-Level) at Pomegranate
- Full-Stack Software Developer at ClearPoint Strategy
- ColdFusion developer needed!!! at Ansira
That is pretty awesome, nice find!
Yeah, CF9 is chock full of yummy goodness.
Cool find. Thats going to help me next week!
Good timing then.
for what stands the # in
But we determine the variables.
Is it possible to let the user input her nam, so that coldfusion gets the information like php with $_POST['inputname'] ??
Or is your example even just for the determine variables ?
The # signs are used to evaluate a ColdFusion variable. As such, the #girls.girls.girl# evaluates to the pseudo node XML wrapper and passes that to the "array" attribute.
You might see similar things in other languages like:
You could certainly use a user-defined variable to get a girl's name. You would do this in either as part of a dynamic XPath query or as part of the dot-notation turned array notation:
#girls[ FORM.inputname ].girl#
Here, the "inputname" would be a FORM variable posted by the user and used to navigate the XML document. Of course, in my example, the "inputname" would have to evaluate to "girls" in order for it to make sense.