Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 00:26
Show Gist options
  • Save bennadel/9752387 to your computer and use it in GitHub Desktop.
Save bennadel/9752387 to your computer and use it in GitHub Desktop.
Stripping XML Name Spaces And Node Prefixes From ColdFusion XML Data (To Simplify XPath)
<!--- Store the XStandard SOAP XML. --->
<cfsavecontent variable="strXml">
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<doDirectorySearch
xmlns="http://xstandard.com/2004/web-services">
<lang>en</lang>
<searchFor>Smith</searchFor>
<filterBy>staff</filterBy>
</doDirectorySearch>
</soap:Body>
</soap:Envelope>
</cfsavecontent>
<!---
Parse the XStandard SOAP request XML into a ColdFusion XML
document object. Be sure to trim the XML so that it
parses properly.
--->
<cfset xmlRequest = XmlParse(
strXml.Trim()
) />
<!--- Search for the "searchFor" XML node using XPath. --->
<cfset arrSearchNodes = XmlSearch(
xmlRequest,
"//searchFor"
) />
<!--- Search for "searchFor" XML nodes using XPath. --->
<cfset arrSearchNodes = XmlSearch(
xmlRequest,
"//*[name()='searchFor']"
) />
<!---
Strip out the tag prefixes. This will convert tags from the
form of soap:nodeName to JUST nodeName. This works for both
openning and closing tags.
--->
<cfset strXml = strXml.ReplaceAll(
"(</?)(\w+:)",
"$1"
) />
<!---
Remove all references to XML name spaces. These are node
attributes that begin with "xmlns:".
--->
<cfset strXml = strXml.ReplaceAll(
"xmlns(:\w+)?=""[^""]*""",
""
) />
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Body>
<doDirectorySearch>
<lang>en</lang>
<searchFor>Smith</searchFor>
<filterBy>staff</filterBy>
</doDirectorySearch>
</Body>
</Envelope>
<!---
Parse the XStandard SOAP request XML into a ColdFusion XML
document object. Be sure to trim the XML so that it
parses properly.
--->
<cfset xmlRequest = XmlParse(
strXml.Trim()
) />
<!--- Search for the "searchFor" XML node using XPath. --->
<cfset arrSearchNodes = XmlSearch(
xmlRequest,
"//searchFor"
) />
<!--- Dump out the search results. --->
<cfdump
var="#arrSearchNodes#"
label="searchFor XPath Search Results"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment