At last nights OCCFUG meeting, one of the members asked what is the best way to access the data from nodes in an XML document that have xmlns namespace prefixes. I touched on this topic briefly in the comments of my post about reading XML documents, but I thought I would expound upon it a little more as there doesn't seem to be a lot of solid explanations out there on how to do this in ColdFusion. In a nutshell, the answer is to use XMLSearch() with an xPath expression that utilizes the namespace-uri() function.

For this example we are going to use CFFEED to retrieve weather information from the Yahoo! weather api.

   source = ""
   xmlVar= "myXML">

The myXml variable now contains the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:rdf="" xmlns:itunes="" xmlns:dc="" xmlns:taxo="" version="2.0">
<title>Yahoo! Weather - Fountain Valley, CA</title>
<description>Yahoo! Weather for Fountain Valley, CA</description>
<lastBuildDate>Fri, 26 Sep 2008 12:53:00 GMT</lastBuildDate>
<title>Yahoo! Weather</title>
<title>Conditions for Fountain Valley, CA at 8:53 am PDT</title>
<geo:lat xmlns:geo="">33.71</geo:lat>
<geo:long xmlns:geo="">-117.95</geo:long>
<yweather:condition xmlns:yweather="" text="Partly Cloudy" code="30" temp="66" date="Fri, 26 Sep 2008 8:53 am PDT" />
<yweather:forecast xmlns:yweather="" day="Fri" date="26 Sep 2008" low="65" high="79" text="Partly Cloudy" code="30" />
<yweather:forecast xmlns:yweather="" day="Sat" date="27 Sep 2008" low="64" high="80" text="Partly Cloudy" code="30" />
<description>&lt;img src=""/&gt;&lt;br /&gt;
&lt;b&gt;Current Conditions:&lt;/b&gt;&lt;br /&gt;
Partly Cloudy, 66 F&lt;BR /&gt;
&lt;BR /&gt;&lt;b&gt;Forecast:&lt;/b&gt;&lt;BR /&gt;
Fri - Partly Cloudy. High: 79 Low: 65&lt;br /&gt;
Sat - Partly Cloudy. High: 80 Low: 64&lt;br /&gt;
&lt;br /&gt;
&lt;a href="*"&gt;Full Forecast at Yahoo! Weather&lt;/a&gt;&lt;BR/&gt;
(provided by The Weather Channel)&lt;br/&gt;</description>
<pubDate>Fri, 26 Sep 2008 12:53:00 GMT</pubDate>
<guid isPermaLink="false">92708_2008_09_26_8_53_PDT</guid>

You can see in that xml that there is a "geo" namespace that is used for the latitude and longitude nodes, and there is a namespace called "yweather" that is used in the nodes that hold the current and forecasted weather conditions. XML spaces always have a Uniform Resource Identifier (URI) associated with them, and the URI looks just like a URL that you would use to browse to a web page. You will need to use this URI in your xPath expression in order to find any node that has a namespace prefix.

This example shows how to write an xPath expression to find all the nodes with a specific namespace URI, and how to find nodes within a namespace that have a specific local name. (click here to see the results of this code)

<cffeed source = ""
   xmlVar= "myXML">

<b>Get all the Nodes with the yweather namespace</b>
<cfset yWeatherNodes = xmlSearch(myXML,"//*[namespace-uri()='']")> <cfdump var="#yWeatherNodes#">

<b>Only get the yweather:forecast nodes</b>
<cfset yWeatherForecastNodes = xmlSearch(myXML,"//*[local-name()='forecast' and namespace-uri()='']")> <cfdump var="#yWeatherForecastNodes#">

<b>Get the nodes with the geo namespace</b>
<cfset geoNodes = xmlSearch(myXML,"//*[namespace-uri()='']")> <cfdump var="#geoNodes#">

From there you would just loop through the array of nodes that gets returned by the xmlSearch() function and do whatever you need to with the data. The following example outputs all the data from the yweather:forecast nodes (click here to see the results of this page):

<cffeed source = ""
   xmlVar= "myXML">

<cfset yWeatherForecastNodes = xmlSearch(myXML,"//*[local-name()='forecast' and namespace-uri()='']")> <cfoutput>
<h3>#arraylen(yWeatherForecastNodes)# day weather forecast for 97208</h3>
<cfloop from="1" to="#arraylen(yWeatherForecastNodes)#" index="i">
<cfset forecast = yWeatherForecastNodes[i].XmlAttributes>
   <b>, #dateformat(,"mm/dd/yyyy")#</b><br>
   High: #forecast.high#<br>
   Low: #forecast.low#<br>

XMLSearch() rocks! I remember before it arrived in ColdFusion MX and I would have to do all kinds of nested cfloops to get at nodes that were deep in an xml document. It's a big time saver once you learn how to write the xPath expressions. To learn more about them, visit

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Andrew Maurer's Gravatar Beautiful, Thanks Scott!
# Posted By Andrew Maurer | 9/26/08 9:33 PM
dc's Gravatar an alternative syntax is:
[cfset forecasts = XMLSearch(myXML, "//yweather:forecasts"/]

(note: this doesn't work with the sample xml above unless you move the xmlns:yweather decleration to the root element)
# Posted By dc | 9/27/08 2:21 PM
Home | Blog | Contact | © 2001 - 2021 The ColdFusion Guy - Scott Bennett. All rights reserved.