17 January 2011

I got tired of waiting for Macromedia to fix the bug in XMLSearch, so I wrote this User Defined Function that will bypass the problem with xpath searching when the xmlns attribute is set in the XMLDoc.

The code below uses a regular expression to strip the xmlns attribute out of the XMLDoc before performing the xpath search.

<cffunction name="fixedXMLSearchOld" access="public" returntype="array"> <cfargument name="XMLString" type="Any" /> <cfargument name="xPathString" type="string" /> <cfset var currentXMLString = "" /> <cfset var currentXMLDoc = xmlNew() /> <cfset var myArray = arrayNew(1) /> <cfif isXMLDoc(arguments.XMLString)> <cfset currentXMLString = rereplaceNoCase(toString(arguments.XMLString),myRegEx,"","all") /> </font> <cfset currentXMLString = rereplaceNoCase(arguments.XMLString,myRegEx,"","all") /> </cfif> <cfset currentXMLDoc = XMLParse(currentXMLString) /> <cfset myArray = XMLSearch(currentXMLDoc,xPathString) /> </cffunction></div> I also wrote a version in cfscript.
</font> function fixedXMLSearch(XMLString, xPathString) { var myRegEx = 'xmlns[ ]*=[ ]*"[[:graph:]]+"'; var currentXMLString = ""; var currentXMLDoc = xmlNew(); var myArray = arrayNew(1); if (isXMLDoc(arguments.XMLString)) { currentXMLString = rereplaceNoCase(toString(arguments.XMLString),myRegEx,"","all"); } else { currentXMLString = rereplaceNoCase(arguments.XMLString,myRegEx,"","all"); } currentXMLDoc = XMLParse(currentXMLString); myArray = XMLSearch(currentXMLDoc,xPathString); return myArray; } </cfscript></div>


blog comments powered by Disqus