Ask Ben: Selecting XML Attributes Given Other XML Attributes
Posted September 19, 2007 at 6:39 PM
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:
- Select an attribute node that is part of a element node that has an attribute of a given value.
- Select an attribute node that has a sibling attribute node of a given value.
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 »
- <!--- Define our ColdFusion XML document object. --->
- <cfxml variable="xmlGirls">
-
- <girls>
- <girl
- name="Samantha"
- age="27"
- hair="Blonde"
- />
- <girl
- name="Kim"
- age="32"
- hair="Brunette"
- />
- <girl
- name="Cindi"
- age="25"
- hair="Black"
- />
- </girls>
-
- </cfxml>
-
-
- <!---
- Get the Name attribute nodes of all the girls
- who are brunetted. We are going to be doing this
- by only looking in girl nodes that have a hair
- attribute that is brunette.
- --->
- <cfset arrNodes1 = XmlSearch(
- xmlGirls,
- "//girl[ @hair = 'Brunette' ]/@name"
- ) />
-
- <!---
- Get the Name attribute nodes of all the girls
- who are brunetted. We are going to be doing this
- by getting all name nodes who have a sibling
- attribute node, hair, that is Brunette.
- --->
- <cfset arrNodes2 = XmlSearch(
- xmlGirls,
- "//girl/@name[ ../@hair = 'Brunette' ]"
- ) />
-
-
- <!--- Output the matching nodes. --->
- <cfdump
- var="#arrNodes1#"
- label="Names of Burnette Girls - Method ##1"
- />
-
- <!--- Output the matching nodes. --->
- <cfdump
- var="#arrNodes2#"
- label="Names of Burnette Girls - Method ##2"
- />
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
Post Comment | Ask Ben | Other Searches | Print Page
Newer Post
Convert jQuery XML Documentation To HTML / PDF Using ColdFusion And XSLT
Older Post
ColdFusion 404 Handling Makes Sub Domain Redirects Hard
Reader Comments
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
@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.
Thanks for this example. It was exactly what I was looking for. Sometimes it's impossible to use XPath without a resources of many solid examples.
@John,
Awesome! Glad to help. If you run into any more issues with XPath, please drop me a line and I'd be happy to whip up a good demo or tutorial.
Saved me AGAIN! 2 in one day...
it's getting to the point that I go to bennadel.com first, then the cfdocs.
(in most cases google finds Ben's stuff first anyway!)
@Michael,
Ha ha, that's awesome. Always glad to help!
thanks, that was very helpful
Seems like I'm missing something. If I take this code and put it in a .cfm file and try to run it I get this error:
Detail Premature end of file.
ErrNumber 0
ExceptionMessage Premature end of file.
Message An error occured while Parsing an XML document.
@Tom,
That's odd. Looks like a malformed XML problem. Make sure you aren't missing any of the ">" closing brackets or something.





