tag:blogger.com,1999:blog-31425501.post-1154455865034591562006-08-01T13:08:00.002-05:002008-03-11T11:07:01.619-05:00XMLSearch, XPath, and XML namespaces in ColdFusionSample code used in this posting is ColdFusion-specific, but the XPath syntax will likely apply to many XPath implementations in other languages. <br />I was looking a specific set of elements in a SOAP response and kept getting an empty array returned from XMLSearch. Turns out this was related to how the namespaces in the response were defined and/or assigned to certain elements. (see <a href="http://www.talkingtree.com/blog/index.cfm/2005/11/18/XmlSearchNoNameNamespace">this Talking Tree posting</a>) <br />The information in the Talking Tree posting helped me identify my problem, but in my case, it wasn't about noname namespaces. In fact, what if you want to find all elements of a given name and don't care about the namespace or even where it lies in the hierarchy? In the example below, I want to quickly retrieve all of the Response elements. Note that one Response element is a child of Other, while the remaining are direct descendants of ResponseList. <br /><pre><br />&lt;ResponseList xmlns="urn:shama.lama.dingdong.net"&gt;<br />&lt;Response&gt;<br />&lt;ns1:success xmlns:ns1="urn:core.shama.lama.dingdong.net"&gt;true&lt;/ns1:success&gt;<br />&lt;baseRef internalId="1234" xmlns:ns2="urn:core.shama.lama.dingdong.net"/&gt;<br />&lt;/Response&gt;<br />&lt;Response&gt;<br />&lt;ns3:success xmlns:ns3="urn:core.shama.lama.dingdong.net"&gt;false&lt;/ns3:success&gt; <br />&lt;ns3:statusDetail type="ERROR"&gt;<br />&lt;ns3:code&gt;USER_ERROR&lt;/ns3:code&gt;<br />&lt;ns3:message&gt;That record does not exist.&lt;/ns3:message&gt;<br />&lt;/ns3:statusDetail&gt;<br />&lt;/ns3:status&gt;<br />&lt;baseRef internalId="4421" xmlns:ns4="urn:core.shama.lama.dingdong.net"/&gt;<br />&lt;/Response&gt;<br />&lt;Other&gt;<br />&lt;Response&gt;<br />&lt;ns3:success xmlns:ns3="urn:core.shama.lama.dingdong.net"&gt;false&lt;/ns3:success&gt; <br />&lt;ns3:statusDetail type="ERROR"&gt;<br />&lt;ns3:code&gt;RECORDNOTFOUND_ERROR&lt;/ns3:code&gt;<br />&lt;ns3:message&gt;That record does not exist.&lt;/ns3:message&gt;<br />&lt;/ns3:statusDetail&gt;<br />&lt;/ns3:status&gt;<br />&lt;/Response&gt;<br />&lt;warning&gt;Import timed out briefly and process was restarted. No further errors were reported.&lt;/warning&gt;<br />&lt;/Other&gt;<br />&lt;/ResponseList&gt; <br /></pre><br />Now if it weren't for the namespaces, I could simply use the following: <br /><pre>&lt;cfset MyArray = XMLSearch(MyXMLDoc, "//Response")&gt; </pre><br />...but in this case that would return an empty array. <br /><br />Ok, but what about the noname namespace syntax: <br /><pre>&lt;cfset MyArray = XMLSearch(MyXMLDoc, "//:Response")&gt; </pre><br />That's fine if there's an actual noname namespace assigned to that element, but in this case there isn't one assigned. <br /><br />After trying countless variations of XPath syntax, I still couldn't get anything other than a blank array returned. I became desperate and quickly wrote a function that strips all of the namespace definitions and labels out of the XML, then searched on the results of that. But I felt this was rather convoluted and added too much overhead. Surely there was a better way. I kept searching, and lo and behold, came across the local-name function. If you want to ignore namespaces and hierarchical context completely, you can search by the local name of the element: <br /><pre>&lt;cfset MyArray = XMLSearch(MyXMLDoc, "//*[local-name()='Response']" </pre><br />And now you have your array containing the three Response elements.<br /><br />March 11, 2008 UPDATE: Ryan commented that he had tried the syntax below with similar success. I have not tried this myself, but give it a shot:<br /><pre>&lt;cfset MyArray = XMLSearch(MyXMLDoc, "//*:Response")&gt;</pre>Jeremy "King Skidz" Gibbensnoreply@blogger.com