Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion

ColdFusion: Article

The XPath Factor

XML and XPath

This example brings about a problem. There is both a challenger, Roberto Donna, and an Iron Chef, Mario Batali, who each cook Italian food. However the previous example only returns Roberto Donna since the expression explicitly lists "//Challenger" before the "@food='Italian'" clause. XPath has a logical OR operator which will give us a work around to this problem. The pipe "|" character will concatenate two or more expressions together.

   <cfset XPathResult = xmlSearch(ironChefXMLObject,
"//Challenger[@food='Italian']|//IronChef[@food='Italian']")>

This expression is really composed of two separate sub-expressions: "//Challenger[@food='Italian']" and "//IronChef[@food='Italian']." The first sub-expression returns the complete "Challenger" node, including its children and descendants, for all chefs who cook Italian food. The second sub-expression returns the "IronChef" node, including its children and descendants, for each Iron Chef who cooks Italian food. The first sub-expression returns one node set, the second four node sets, thus creating a five-node set that ColdFusion returns as a five-element array.

As you can see XPath has a full set of operators. Here is a list of all of the XPath operators and their description:

| Node set concatenation.
+ Addition
- Subtraction
* Multiplication div Division
= Equal
!= Not Equal
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
or Or
and And
mod Modulus

Many of the operators have multiple uses, however, remember that since ColdFusion can only return node sets, some common uses of these operators cannot be used. For example, you cannot add together the total scores of all the Iron Chefs and see which one had the highest overall score for the season. To do this you will have to leverage other features of ColdFusion, which are out of the scope of this article. So far we have written expressions based off of known XML schemas. Oftentimes you will not know the complete schema before you need to manipulate the data within the file. XPath has three built-in wildcard handlers for this very situation: *, @,* and node().

<cfset XPathResult = xmlSearch(ironChefXMLObject, "//Challenger/*")>
<cfset XPathResult = xmlSearch(ironChefXMLObject, "//Challenger/@*")>
<cfset XPathResult = xmlSearch(ironChefXMLObject, "//Challenger/node()")>

"//Challenger/*" will return the nodes and their values of all the children and descendants of "Challenger," but not "Challenger" itself. It also does not return any of the attributes or attribute values of any of the node sets. This is different than "//Challenger," which will return "Challenger" and it children/descendants, as well as the attributes of those nodes. "//Challenger/@*" will return the same node set, however it will only return the attributes of those nodes. If you want to return all of the nodes and attributes of those nodes, you would use the node() function, ("//Challenger/node()").

Running XPath expressions against XML documents in ColdFusion does not change the original XML document. This allows developers to experiment freely when trying to learn XPath, without worrying about the consequence (as they might when playing with SQL statements). There are scores of reference materials dedicated to the ins and outs of XPath, just be careful when investing any money into reference materials, remembering that ColdFusion does not support all that XPath has to offer. Do not be put-off by ColdFusion's crippled XPath engine though, Macromedia has made sure that the most useful and timesaving features are available to you. If you would like more information on XPath and how ColdFusion implements it, you can check out both the CFML reference guide and the ColdFusion developer's guide - available on macromedia.com. The W3C also has two great XPath references, the XML Path Language guide (www.w3.org/TR/XPath) and the W3C Schools XPath tutorial (www.w3schools.com/XPath).

More Stories By Nik Molnar

Nik Molnar is a ColdFusion/Flex developer with over seven years experience. He has led teams through the development of enterprise applications for the mortgage, sports ticketing, and stock industries. He is an amateur Iron Chef and posts regularly at his blog: foodDuo.com. He lives with his wife Katy and his dog Jacques in Orlando, Florida.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.