Home Why I stopped caring and learned to hate XPath
Post
Cancel

Why I stopped caring and learned to hate XPath

Yesterday I came across another one of XPath's little quirks that cost me up to an hour of painstaking step-by-step troubleshooting to narrow down: comparing the value of a boolean element to one of the boolean functions (either true() or false()), also known as a subset of boolean expressions.

I had an element of type boolean buried in an XML document and my initial expression to test its value was like this:

/path/to/boolean/element = true()
The expression is so simple and innocent-looking that (gasp!) there just couldn't be anything wrong with it. The path was expressed correctly, I was using the right operator and I wanted to see if the boolean element had a boolean value of true. Except, of course, it just didn't work as expected and the code just kept taking a wrong turn.

I admit that I had previously seen this tested another way, namely like:
 /path/to/boolean/element = 'true'
Notice how the comparison is now done against a string and not a boolean type. And it works. I thought it was just the engine or the specification trying to make life easier for programmers, some sort of condescending you're doing it wrong but we understand what you're trying to accomplish here so we'll just run along with it thing going on here. And why not believe that since, after all, at least some features of XPath were catered to non-programmers? So, me being me, I wanted to do it how I believed was the true, correctly typed way of course.

And like I said, it doesn't work.

If you go back to the link on boolean expressions you'll notice this line:

If one object to be compared is a node-set and the other is a boolean, then the comparison will be true if and only if the result of performing the comparison on the boolean and on the result of converting the node-set to a boolean using the boolean function is true.
 This is the scenario I'm talking about. Let's follow through the link to the boolean function:
 The boolean function converts its argument to a boolean as follows:
  • a node-set is true if and only if it is non-empty
Mother of god... My expression wasn't testing the value of my boolean element - it was checking to see if the result of my xpath returned any nodes at all! Gasp! Going back to the section on boolean expressions reveals this:

If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true.
So hold on a minute.... work with me here. If I compare my xpath that selects a boolean element against a string then the actual value of said element is considered, but when I compare that same xpath against a boolean type then the actual value of the element is irrelevant?

What insane world are we living in? What was the genius behind this design decision?
This post is licensed under CC BY 4.0 by the author.

Those facepalm moments...

Performance comparison: Linked-List vs. Vector [Java]