I find Xpath very powerul for searching through a XML document. I have used it extensively in some of my projects and it might be worth it to post some of the examples here. For this example, I have used the books.xml sample xml file which can be found here.
Following are some examples of what we can do with Xpath in Coldfusion. I have tried to keep the variable names self-explanatory so it should easy to figure out which statement is doing what.
<cffile action="read" file="#ExpandPath('books.xml')#" variable="myBooks">
<cfset myxml="XmlParse(myBooks)">
<cfscript>
allbooks = XmlSearch(myXml, "catalog/book");
firstBook = XmlSearch(myXml, "catalog/book[1]");
lastBook = XmlSearch(myXml, "catalog/book[last()]");
secondLastBook = XmlSearch(myXml, "catalog/book[last()-1]");
countBooks = XmlSearch(myXml, "count(catalog/book)");
sumPriceAllBooks = XmlSearch(myXml,sum(catalog/book/price)");
bookAuthors = XmlSearch(myXml,"catalog/book/author");
searchByTitle = XmlSearch(myXml,catalog/book[title='Midnight Rain']/title");
searchByAuthor = XmlSearch(myXml,catalog/book[genre='Fantasy']/title");
searchById = XmlSearch(myXml,catalog/book[@id='bk109']/title");
</cfscript>
If you want to try this code out, make sure you download the books.xml and keep it in the same folder as you keep the above code in.
More Xpath samples can be found here .
#1 by Ben Nadel on November 2, 2007 - 12:57 pm
Quote
I love XPath in ColdFusion, but you rocked my world here a little bit. I had thought that XmlSearch() could ONLY return an array of matching nodes. I had no idea that it could return simple values as you are doing with count() and sum(). This is very good to know, thanks!
#2 by Ben Nadel on November 2, 2007 - 1:08 pm
Quote
Hey, I just ran some of these examples on my local machine and I found something interesting. The non-node list examples (sum, count, etc) don’t work in CF7, only CF8.
#3 by admin on November 2, 2007 - 1:08 pm
Quote
Yes there is more to it than just the normal matching nodes stuff.
here is a list I normally refer to.
http://www.w3schools.com/xpath/xpath_functions.asp
#4 by admin on November 2, 2007 - 1:14 pm
Quote
Thats interesting. When I was trying this code, I was using Coldfusion 8 as well. I am gonna find out why they dont work in CF7.
#5 by Ben Nadel on November 2, 2007 - 1:24 pm
Quote
Looking at the live docs, it looks like non-nodelist support was only added in CF8. Also, I love the W3Schools site.
#6 by admin on November 2, 2007 - 1:39 pm
Quote
hmm….. I couldnt get the min() function to work either. Not sure if that is not supported or something I was doing wrong. It just keeps saying “function min not found” .
#7 by Dan Vega on November 2, 2007 - 2:46 pm
Quote
Great article, I was unaware of this as well. I found an error while running your example but there is a quick fix.
TO
#8 by admin on November 2, 2007 - 3:15 pm
Quote
Thanks Dan. Do you mind sharing what the error was? and the fix as well
#9 by admin on November 3, 2007 - 3:23 pm
Quote
Just found out that we can check whether or not a function is available for querying in XML.
e.g.
we can do this.
<cfset isFunction = XmlSearch(myXml, “function-available(‘min’)”) />
This returns a boolean value , in this case its NO, but this answers my question of why the min function was not working.
I also found out the list of functions that can be used for querying.
http://www.stylusstudio.com/docs/v62/d_xpath15.html#wp1455341
Hope this is helpful.
#10 by james beuthling on March 7, 2009 - 1:38 am
Quote
I think the error is the missing semi colons
11 : sumPriceAllBooks = XmlSearch(myXml,sum(catalog/book/price)”);
my guess !_! – hey dan
#11 by james beuthling on March 7, 2009 - 3:39 am
Quote
I am having problems with two of tags–
Im not on 8 is this why?
#12 by Anuj Gakhar on March 9, 2009 - 2:19 pm
Quote
@James, The SUM and COUNT are actually specific to CF8 only, as in the comments above.
#13 by anon on November 28, 2009 - 11:06 pm
Quote
Another Xpath engine you might be interested in knowing:
http://vtd-xml.sf.net