Select Any XML Node With A Given Value Using ColdFusion And XPath

Posted July 24, 2007 at 6:24 PM

Tags: ColdFusion

Earlier today, on the CF-Talk list, Brad Wood was asking about selecting any node from an XML document based on its text value (text child node). In my ColdFusion, XmlSearch, and XPath tutorial I did a bunch of stuff with "any node" examples and also with text-based examples, but I didn't do anything that combined the two. I didn't know if ColdFusion's XmlSearch() method would hiccup if you tried to use "//*" in conjunction with a text() predicate:

 Launch code in new window » Download code as text file »

  • <!--- Build an XML document object. --->
  • <cfxml variable="xmlData">
  •  
  • <root>
  • <a1>Meep</a1>
  • <a2>Meep</a2>
  • <a3>
  • <b1>Dink</b1>
  • <b2>Meep</b2>
  • </a3>
  • <a4>Blam</a4>
  • </root>
  •  
  • </cfxml>
  •  
  •  
  • <!---
  • Get all nodes in the XML document that have the
  • child text node, "Meep". We don't care where the
  • node is or they are related... we only care about
  • this inner text value.
  • --->
  • <cfset arrNodes = XmlSearch(
  • xmlData,
  • "//*[ text() = 'Meep' ]"
  • ) />
  •  
  • <!--- Dump out results. --->
  • <cfdump
  • var="#arrNodes#"
  • label="All Nodes By Text Value"
  • />

I was pleased to see that this functions exactly like I hoped it would, giving us the following CFDump output:


 
 
 

 
XML Nodes Found Based On Their Text Value  
 
 
 

Notice that it found the a1, a2, and b2 element nodes based purely on their nested text node. Good stuff.

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

Jul 24, 2007 at 10:57 PM // reply »
49 Comments

Good stuff! FYI - There is a great Firefox extension called "Xpather" that is great for displaying the Xpath info for elements on a page...


Jul 25, 2007 at 8:02 AM // reply »
6,516 Comments

Yeah, I think someone else mentioned XPather too. I really gotta check that out. FireFox is the most badass browser.


db
Jul 25, 2007 at 8:28 AM // reply »
3 Comments

firebug does a nice job of showing xpath - open the console and click the 'dom' tab. when you mouseover the elements (in blue) in the list on the right, the xpath is displayed, and the element is hilited on the page. right click to copy the xpath.


Jul 25, 2007 at 8:38 AM // reply »
6,516 Comments

@DB,

Good call. I just tried it (in the email I got) and it works like a charm:

/html/body/div[2]/div[2]/div[3]/table/tbody/tr/td/div/div[2]/div[4]/
table/tbody/tr/td/div/div/div/p[2]

... I gMail as a lot of stuff going on in it's pages :)

What would I do without FireBug??


Jul 25, 2007 at 8:56 AM // reply »
49 Comments

Sweet - I think Xpather is easier to use - it's more direct - I select the element, right-click and select "Show in Xpather" - with Firebug I have to dig a bit - but thats useful for finding elements that may not be visible on the page. Good stuff!


db
Jul 25, 2007 at 3:03 PM // reply »
3 Comments

yeah, those lists can be too much information, sometimes. if you right click on the page and select 'inspect element', you get more specific info. a bar at the top of the console has the element and its parents, and the xpath info.


Jun 7, 2009 at 3:34 PM // reply »
1 Comments

if you right click on the page and select 'inspect element', you get more specific info. a bar at the top of the console has the element and its parents, and the xpath info.


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 »