Ask Ben: Selecting XML Attributes Given Other XML Attributes

<!--- 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"
	/>

For Cut-and-Paste