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





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,516 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.


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,516 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,516 Comments

@Michael,

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


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

thanks, that was very helpful


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »