Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the jQuery Conference 2009 (Cambridge, MA) with:

Trailing Slashes In XPath And Multiple Node Sets

By Ben Nadel on
Tags: ColdFusion

When I gave my introductory presentation on the use of XPath and XmlSearch() in ColdFusion, I stated that the use of a trailing slash at the end of your XPath path was optional. For example, the XPath:

//datum[ text() = '1.1' ]

... would return the same nodes as this XPath value that ends with a trailing slash:

//datum[ text() = '1.1' ]/

As such, I just assumed that this trailing slash was optional. And, it never caused a problem ... until this weekend. I was trying to calculate two simultaneous node sets using the "|". This is like an "or" statement between two paths used when searching the ColdFusion XML document object model. For example, I was trying to do this:

//datum[ text() = '1.1' ]/ | //datum[ text() = '3.2' ]/

Notice that both XPath values have trailing slashes and are separated by the pipe (|). When faced with this path structure, ColdFusion kept throwing the following error:

An error occured while Transforming an XML document. A location step was expected following the '/' or '//' token.

After figuring out what the problem is, the error seems obvious, but I have been thinking about it all weekend and just came up with the answer. Removing the trailing slashes fixes the problem:

//datum[ text() = '1.1' ] | //datum[ text() = '3.2' ]

This XmlSearch() function now executes fine and returns a node set that contains the nodes found from both XPath values. I am not sure why the trailing slash works sometimes but not other, but I guess going forward, I should just leave it out.




Reader Comments

That's interesting. I'll have to look over the XPath spec and play around with some other tools to see why that would happen.

As a side note, I think your expression is probably better written as:

//datum[ text() = '1.1' or text() = '3.2' ]

Assuming the same datum.

Reply to this Comment

@Elliott,

You are right, my example would be better done as a compound predicate. However, ultimately, what I was trying to do when I came across this problem was select both a parent node and it's child into the top level node set. There was logic in the XPath to determine if the child node existed (as only wanted to get parents that had children).

Ultimately, I wanted to be able to loop over the node list with step="2" and then [i] == parent and [i+1] == child. It would have made the looping logic very easy.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.