Skip to main content
Known Participant
April 16, 2009
Answered

Xpath with XML from a Web Service

  • April 16, 2009
  • 2 replies
  • 713 views

HI,

I have been hacking away pulling data from a webservice and have managed to get the results that I need so I am now at the stage where I want to do something useful with the XML that is returned so I have been playing with Xpath.

As I understand it I can use Xsearch to pull nodes from the XML object rather than have to write the XML out to a file and then search through the file. The problem I am having is that when I use Xsearch it doesn't extract the data that I expect it to.

Here is the XML that is returned from the web service which I assign to the variable xmlReturned:

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

     <soap:Body>

          <GetChildLocationsResponse xmlns="http://tempuri.org/VillarentersWebService/villa_search">

               <GetChildLocationsResult>

                    <RequestedParentID>3924</RequestedParentID>

                    <VRF />

                     <ChildLocations>

<Location>

<LocationRef>10</LocationRef>

<LocationDescription>Argentina</LocationDescription>

<ParentID>3924</ParentID>

</Location>

<Location>

<LocationRef>30</LocationRef>

<LocationDescription>Brazil</LocationDescription>

<ParentID>3924</ParentID>

</Location>

</ChildLocations>

</GetChildLocationsResult>

</GetChildLocationsResponse>

</soap:Body>

</soap:Envelope>

So when I use :

<cfset locDescription = XmlSearch(xmlReturned, "//LocationDescription") />

<cfdump var="#locDescription#">

I am expecting to see an array with 2 elements, Argentina and Brazil. What I actually get is an empty array. I have tried using a really handy online Xpath checker at http://www.zrinity.com/xml/xpath/index.cfm and I get the same result. I tried a few different searches but each time the array is empty.

Does anyone know why it isn't pulling the requested nodes into the array? Am I missing something very simple here?

This topic has been closed for replies.
Correct answer ilssac

Or you could account for the namespaces in your xmlsearch xpath properties.

Here is the first link I got from a Google search for "xmlsearch namespace"

http://www.aftergeek.com/2006/08/xmlsearch-xpath-and-xml-namespaces-in.html

2 replies

ilssac
ilssacCorrect answer
Inspiring
April 16, 2009

Or you could account for the namespaces in your xmlsearch xpath properties.

Here is the first link I got from a Google search for "xmlsearch namespace"

http://www.aftergeek.com/2006/08/xmlsearch-xpath-and-xml-namespaces-in.html

Known Participant
April 16, 2009

ianskinner wrote:

Or you could account for the namespaces in your xmlsearch xpath properties.

Here is the first link I got from a Google search for "xmlsearch namespace"

http://www.aftergeek.com/2006/08/xmlsearch-xpath-and-xml-namespaces-in.html

WOW, that certainly simplifies things a bit! That is one elegant solution, I'll give it a try. Thanks.

Inspiring
April 16, 2009

I've had issues in both Flex and CFML with namespaces in the SOAP XML returned from a Web Service. It might be worth trying to remove (strip out or otherwise) the SOAP namespace and run your test again. You could even just manually use the XML from your post (strip out the soap: stuff) and try to navigate the document with XPath to see if that took care of it. I know it took care of some XML parsing and traversing for me in the past.

Known Participant
April 16, 2009

Thanks for that suggestion. I tried stripping out the namespaces and Xpath did return what I was looking for. I used:

<cfset xmlReturned = ReReplaceNoCase(#xmlReturned#, "<soap:[^>]*>|</soap[^>]*>|<getChildLocationsResponse[^>]*>|</getChildLocationsResponse[^>]*>", "", "ALL" ) />

which isn't the most elegant solution but it has done the trick for now.