Ask Ben: Selecting All Text Nodes Of An XML Document

Posted August 13, 2007 at 7:50 PM by Ben Nadel

Tags: ColdFusion, Ask Ben

I like all your xpath stuff you have been doing in ColdFusion. Can you help get the text values of my xml document?

In my presentation on introductory XPath in ColdFusion, I neglected to cover selecting text nodes, but rest assured, this is a fairly easy process. Rather than selecting the node name or the attribute name, all you need to do is select the "text()" value. To explore this, let's first create an XML document:

  • <!--- Build ColdFusion XML object. --->
  • <cfxml variable="xmlImages">
  • <cfoutput>
  •  
  • <images>
  • <image>
  • <url>http://celebslam.buzznet.com/wp-content/uploads/2007/08/carmen-electra-tiny-bikini-14.jpg</url>
  • <desc>Carmen Electra taking off her shirt, revealing a skimpy bikini.</desc>
  • </image>
  • <image>
  • <url>http://celebslam.buzznet.com/wp-content/uploads/2007/08/carmen-electra-tiny-bikini-21.jpg</url>
  • <desc>Carmen Electra walking around in bikini on the beach.</desc>
  • </image>
  • <image>
  • <url>http://celebslam.buzznet.com/wp-content/uploads/2007/08/carmen-electra-tiny-bikini-17.jpg</url>
  • <desc>Carmen Electra playing chicken in a bikini with some other girls in bikinis.</desc>
  • </image>
  • </images>
  •  
  • </cfoutput>
  • </cfxml>

Now, we have a valid XML document object in our ColdFusion variable, xmlImages. We can use XmlSearch() to select all of our text nodes:

  • <!---
  • Select all the text nodes using the text()
  • path construct.
  • --->
  • <cfset arrNodes = XmlSearch(
  • xmlImages,
  • "//text()"
  • ) />
  •  
  • <!--- Dump out matching text nodes. --->
  • <cfdump
  • var="#arrNodes#"
  • label="Text Nodes: //Text()"
  • />

Running the above code, the "//" XPath construct means "anywhere in the XML document." Therefore, this will select all text nodes, giving us the following CFDump output:


 
 
 

 
ColdFusion XPath To Select All Text Nodes In XML Document  
 
 
 

As you can see, we are getting a whole lot of text nodes, not necessarily just the ones we expected - the URL and DESC nodes. Every XML node contains a text node even if it also has nested element (tag) nodes. Therefore, we are getting text nodes for the IMAGES element node as well as for each of the IMAGE nodes.

Now, maybe this is what you want to do, but I am guessing it is not. For the benefit of learning, I am going to assume that we only want to get the text nodes for the URL and DESC element nodes. For that, all we need to do is modify the XPath to narrow down the possible nodes. At first, I was tempted to try and just get any text node that has a length:

  • <!--- Select all the text nodes that have a length. --->
  • <cfset arrNodes = XmlSearch(
  • xmlImages,
  • "//*[ text() != '' ]/text()"
  • ) />

This might look intuitive, but it actually ends up returning all the text nodes in the document. The problem is that our original XML document has a lot of white space. This white space gives the TEXT value of every element node some length. Therefore, we cannot select text nodes based purely on length (had we had no intertag white space, I believe that this would have worked).

To get more accurate results, we need to start narrowing down our path a little more effectively. One method for this would be to select the text of any element node based purely on the node name:

  • <!---
  • Select all the text nodes that are direct
  • descendant of element nodes that have the
  • name url or desc.
  • --->
  • <cfset arrNodes = XmlSearch(
  • xmlImages,
  • "//*[ name() = 'url' or name() = 'desc' ]/text()"
  • ) />
  •  
  • <!--- Dump out matching text nodes. --->
  • <cfdump
  • var="#arrNodes#"
  • label="Get Text Of Nodes URL or DESC"
  • />

Here, we are using an XPath predicate that requires our parent node's names to be either "url" or "desc". Then, once we have those nodes, we are selecting the text(). This gives us the following CFDump output:


 
 
 

 
ColdFusion XPath To Select All Text Nodes Of Specific Nodes (By Name)  
 
 
 

If we don't necessarily know the names of the nodes we want (although I am not sure what that would happen), we can also use a non-direct descendant relationship. In this next example, we are going to select the text nodes of all distant descendants of the image element nodes:

  • <!---
  • Select all the text nodes that are descendants
  • of an image node.
  • --->
  • <cfset arrNodes = XmlSearch(
  • xmlImages,
  • "/images/image/*/text()"
  • ) />
  •  
  •  
  • <!--- Dump out matching text nodes. --->
  • <cfdump
  • var="#arrNodes#"
  • label="Get Text Of Nodes IMAGE descendants"
  • />

This gives us the following CFDump output:


 
 
 

 
ColdFusion XPath To Select Text Nodes As Descendants Of Images  
 
 
 

I wish that ColdFusion's XmlSearch() supported a bit more of the XPath functions, but I hope that this helps point you in the right direction.




Reader Comments

Tim
Aug 24, 2007 at 5:28 PM // reply »
10 Comments

What would be your take on this:

If you have an XML Doc
<employees>
<employee>
<empid>123</empid>
</employee>
<employee>
<empid>456</empid>
</employee>
</employees>

I want a list of empid values. I can use XMLSearch to get an array of empids, and loop over it to listappend the values, but I dont want to have to loop over the nodes. I really wanted to use StructFindKey(myxml,"empid","all") but that function cant be run against xml objects.

Know of a low-intensive way to get a list of empids?


Aug 24, 2007 at 5:56 PM // reply »
11,238 Comments

@Tim,

Not sure if there is an ColdFusion supported XPath way to do this. My guess is that you are gonna need a UDF to do this. I will write one for you :)


Aug 25, 2007 at 2:10 PM // reply »
11,238 Comments

@Tim,

I got a little CFC together that I will post on Monday AM that you might find very useful.


Aug 27, 2007 at 7:12 AM // reply »
11,238 Comments

@Tim,

Take a look at this: http://www.bennadel.com/index.cfm?dax=blog:925.view


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 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools