XMLSearch - No Results Found / Empty Array Solution
Just a quick post while it's fresh on my mind as I'm sure this is going to crop up in the future as I don't often handle XML in Coldfusion.
Basically here is the scenario. You have an XML file which isn't that complicated in structure but whatever you do XMLSearch just won't return the results you expect - it appears broken.
Here is an example from the Spotify API:
<artists xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1">
<opensearch:Query role="request" startPage="1" searchTerms="Ash"/>
<opensearch:totalResults>95</opensearch:totalResults>
<opensearch:startIndex>0</opensearch:startIndex>
<opensearch:itemsPerPage>100</opensearch:itemsPerPage>
<artist href="spotify:artist:2evydP72Z45DouM4uMGsIE">
<name>Ash</name>
<popularity>0.72515</popularity>
</artist>
<artist href="spotify:artist:77zwstbi3x1IxnbDFg6uns">
<name>Wishbone Ash</name>
<popularity>0.67812</popularity>
</artist>
</artists>
Ok, in the above let's say I wan't to get all artists from the XML using xmlSearch.
The usual way would be to specify the following:
<cfset artists = xmlSearch(xml,"/artists/artist")>
This *should* work fine - top level 'artist' node, followed by some 'opensearch' and 'artist' nodes. We should now have an array of artist nodes in our artists variable. But we don't. Why?
Well the simple answer is, look closely at the artist node. Notice the xmlns attributes? They define an XML namespace and throw the whole thing out of whack. Ok, it's part of the spec but to get around this with XMLSearch we need to change our call to the following:
Simple change but this tells the command to ignore the namespace and just return all 'artist' nodes that sit underneath the 'artists' node.
A simple distinction but one that can save hours of hair tearing. :)







http://fusion.dominicwatson.co.uk/2008/03/xmlsearc...
Ah excellent tip - thank you.
Another gotcha to look out for when using XMLSearch.
Your BetterXml CFC sounds excellent. I'll be grabbing that to add to the toolbox me thinks.