Earlier today, someone asked me about searching XML documents in such a way that he only wanted to select a tag attribute if the parent tag had another attribute set to a given value. This is actually a simple task with the use of a Predicate. As a review from my ColdFusion XPath and XmlSearch() presentation, predicates are XPath constructs contained in square brackets that filter the results of the returned nodes. Predicates need to result in a boolean-true in order for the selected node to be returned in the final node set.
In our case, there are two ways we can look at the problem that have slightly different XPath values. We want to:
While these might sound like the same thing, and do, in fact, result in the same node set, they require different XPath values. Both of these situations are demonstrated in this example:
Launch code in new window » Download code as text file »
Notice that in our first ColdFusion XmlSearch() call, our XPath value is first limiting on the Girl node, using a predicate that requires the Hair attribute to be Brunette. Then in our second ColdFusion XmlSearch() call, our predicate is requiring a sibling Hair attribute with no explicit mention of the parent tag (other than by relative relationship).
Running the above tag, we get the two CFDump outputs:
| | | | ||
| | ![]() | | ||
| | | |
| | | | ||
| | ![]() | | ||
| | | |
As you can see, both return the proper Name attribute, Kim. Now, is there a difference between the two different XPath values used? From a readability standpoint, I think the first one is better. From a performance standpoint, I am going to assume that the first one is also a better choice. Just as with a SQL WHERE clause, I think in an XPath statement, you are gonna get better performance by putting the most limiting statements first; filtering on the Girl node will result in less Name attribute node evaluations and therefore might perform better.
Hope that helps a bit.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Convert jQuery XML Documentation To HTML / PDF Using ColdFusion And XSLT
ColdFusion 404 Handling Makes Sub Domain Redirects Hard
Ben,
I was looking at doing this exact same thing. How would it work if I wanted to test/search on two variables say Brunette and age = 32
Posted by Alan Johnson on Sep 21, 2007 at 12:17 PM
@Alan,
XPath supports some AND/OR logic in the predicates:
//girl[ @hair = 'Brunette' and @age = '32' ]/@name
Notice that the "and" is lowercase; this is required. An uppercase AND will not work properly.
Posted by Ben Nadel on Sep 21, 2007 at 12:36 PM