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,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 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »