If you find yourself with some XML, trying to do an XPath query using the ColdFusion
XMLSearch() on it, and the results are always empty, make sure you are not getting caught out by XML namespacing.
For example, take this simple XML:
<MyNode xmlns="http://www.mydomain.com/MyNode"> <SubNode active="1">Some Data</SubNode> <SubNode active="1">More Data</SubNode> </MyNode>
If do a search for the
SubNode as follows:
<cfset arr = xmlSearch(xml, "/MyNode/SubNode") />
You will get no results. The reason is the
xmlns attribute of the document. Until recently, I used to delete the
xmlns attribute from the XML before parsing it as that will work. Not very elegant however. A better approach is to use the following adjusted XPath:
<cfset arr = xmlSearch(xml, "/:MyNode/:SubNode") />
That version ignores the namespacing. I hope that’s helpful. There’s some more info here.