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:
Launch code in new window » Download code as text file »
Now, we have a valid XML document object in our ColdFusion variable, xmlImages. We can use XmlSearch() to select all of our text nodes:
Launch code in new window » Download code as text file »
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:
| | | | ||
| | ![]() | | ||
| | | |
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:
Launch code in new window » Download code as text file »
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:
Launch code in new window » Download code as text file »
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:
| | | | ||
| | ![]() | | ||
| | | |
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:
Launch code in new window » Download code as text file »
This gives us the following CFDump output:
| | | | ||
| | ![]() | | ||
| | | |
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.
Download Code Snippet ZIP File
Comments (4) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
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?
Posted by Tim on Aug 24, 2007 at 5:28 PM
@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 :)
Posted by Ben Nadel on Aug 24, 2007 at 5:56 PM
@Tim,
I got a little CFC together that I will post on Monday AM that you might find very useful.
Posted by Ben Nadel on Aug 25, 2007 at 2:10 PM
@Tim,
Take a look at this: http://www.bennadel.com/index.cfm?dax=blog:925.view
Posted by Ben Nadel on Aug 27, 2007 at 7:12 AM