Using Xpath with Coldfusion

On October 31, 2007, in ColdFusion, by Anuj Gakhar

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 .

Tagged with:  

13 Responses to Using Xpath with Coldfusion

  1. Ben Nadel says:

    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. Ben Nadel says:

    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. admin says:

    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. admin says:

    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. Ben Nadel says:

    Looking at the live docs, it looks like non-nodelist support was only added in CF8. Also, I love the W3Schools site.

  6. admin says:

    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. Dan Vega says:

    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. admin says:

    Thanks Dan. Do you mind sharing what the error was? and the fix as well :)

  9. admin says:

    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. I think the error is the missing semi colons

    11 : sumPriceAllBooks = XmlSearch(myXml,sum(catalog/book/price)”);

    my guess !_! – hey dan

  11. I am having problems with two of tags–
    Im not on 8 is this why?

  12. Anuj Gakhar says:

    @James, The SUM and COUNT are actually specific to CF8 only, as in the comments above.

  13. anon says:

    Another Xpath engine you might be interested in knowing:

    http://vtd-xml.sf.net

Leave a Reply

© 2011 Anuj Gakhar
%d bloggers like this: