Ask a Jedi: How do you get just the string value using XPath in ColdFusion?

This post is more than 2 years old.

John asked:

Hi Ray,

As I work with XML/ColdFusion more and more, and I'm starting to think that I could make things a lot easier on myself with a better understanding of xpath and xmlSearch(). Currently, when I want to get the text or attributes of a node, I use xmlSearch() to return an array and then I access either result[i].xmlText or result[i].xmlAttributes directly.
Is there a way to tell xmlSearch() to return .xmlText or .xmlAttributes directly?

Good question. I began by creating a super simple XML node that contained both text and attributes.

<cfxml variable="testdoc"> <doc> <header> <author>John Doe</author> </header> <content language="en" charset="iso-8859-1"> </content> </doc> </cfxml>

<cfdump var="#testdoc#">

I've got one simple text field for author and a node, content, that has some attributes. So let's first look at how you can get to the John Doe string via a simple search for author:

<cfset test1 = xmlSearch(testdoc, "//author")> <cfdump var="#test1#">

This will search for author anywhere in the document. The result is:

You can get to the text value by simply adding text():

<cfset test1 = xmlSearch(testdoc, "//author/text()")> <cfdump var="#test1#">

Unfortunately, this still returns a complex structure:

Luckily - W3Schools.com once again comes to the rescue with it's excellent XPath docs. If you go into the functions sections you can see all kinds of interesting things you can do with your searches. One of them - fn:string, will return a string value. You need to dump the fn: portion, but to get the name "John Doe", you can just do:

<cfset test1 = xmlSearch(testdoc, "string(//author/text())")> <cfdump var="#test1#">

Pretty easy, right? You can apply the same thing for a search against the language attribute:

<cfset test1 = xmlSearch(testdoc, "string(//content/@language)")>

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by JP posted on 10/29/2010 at 7:26 PM

Thanks Ray! I can't wait to use this technique! Should make my XML parsing work a lot easier! And I think this is a great post because in my research on the subject, I didn't find any examples that were specifically about getting the text or attributes of an xml node directly. Thanks for your help on the subject!

Comment 2 by Grant posted on 10/29/2010 at 7:34 PM

Oh, this is awesome! I always hated XML in CF because of the mile long var names. Thanks Ray!

Comment 3 by Raymond Camden posted on 10/29/2010 at 7:36 PM

Always hated XML in CF? Dude - try it in JS. *shudder* :)