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

Posted July 24, 2007 at 6:24 PM by Ben Nadel

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:

  • <!--- 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.




Reader Comments

Jul 24, 2007 at 10:57 PM // reply »
51 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 »
11,246 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 »
11,246 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 »
51 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 A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools