Ask Ben: Selecting XML Attributes Given Other XML Attributes

Posted September 19, 2007 at 6:39 PM

Tags: ColdFusion, Ask Ben

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:


 
 
 

 
ColdFusion XmlSearch() That Uses XPath  
 
 
 

 
 
 

 
ColdFusion XmlSearch() That Uses XPath  
 
 
 

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  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 21, 2007 at 12:17 PM // reply »
15 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


Sep 21, 2007 at 12:36 PM // reply »
6,371 Comments

@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.


John Shepherd
Dec 2, 2008 at 9:41 PM // reply »
1 Comments

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.


Dec 3, 2008 at 8:01 AM // reply »
6,371 Comments

@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.


Jan 28, 2009 at 4:19 PM // reply »
11 Comments

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!)


Jan 28, 2009 at 4:25 PM // reply »
6,371 Comments

@Michael,

Ha ha, that's awesome. Always glad to help!


Gedali
Jul 21, 2009 at 7:28 AM // reply »
1 Comments

thanks, that was very helpful


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »