Skip to main content
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Angela Buraglia and Dan Short
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Angela Buraglia Dan Short

XPath Support Expanded In ColdFusion 8

By
Published in Comments (2)

I was just reading about some ColdFusion XPath functionality over on Anuj Gakhar's blog. One of the things that he demonstrated was that XmlSearch() could return things like count() and sum() on a node set. I was a little shocked that I had missed this as part of my XPath and XmlSearch() ColdFusion Tutorial. So, naturally, I went and tried it on my test server:

<!--- Create an XML document. --->
<cfxml variable="xmlData">

	<data>
		<datum>1.1</datum>
		<datum>3.2</datum>
		<datum>4.5</datum>
		<datum>7.8</datum>
		<datum>10.9</datum>
	</data>

</cfxml>


<!--- Get the sum of node values. --->
<cfset arrNodes = XmlSearch(
	xmlData,
	"sum( //datum )"
	) />

<!--- Dump out resultant nodes. --->
<cfdump var="#arrNodes#" />

Running this, we get the following output:

27.5

Unbelievable! It worked.

After the shock of having missed this in my initial research wore off a bit, I realized that I was viewing the page as a ColdFusion 8 page (which we have running on an alternate port). Switch the page over to the ColdFusion 7 port and running the page again, I got the following ColdFusion error:

An error occured while Transforming an XML document. Can not convert #NUMBER to a NodeList!

Ahhh, that's what I am used to. Looks like ColdFusion 8 has beefed up the XPath support. And, indeed, looking at the ColdFusion 8 livedocs, I see this in the History:

History
ColdFusion 8: Added support for returning any valid XPath result, not just arrays of XML object nodes.

This is good to know. XPath and XmlSearch() are some very powerful tools to have in the tool belt, and it looks like ColdFusion 8 is making them even more powerful. Man, I love this language!

Want to use code from this post? Check out the license.

Reader Comments

11 Comments

Does anyone happen to know what specific version of xPath ColdFusion 8.0.1 uses?

(Trying to pin down which xPath features CF 8 supports...)

Thanks.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel