Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

XPath can act a little goofy when XML namespaces are involved. Normally, XML parsers recognize imbedded namespaces and know what to do with them, but XPath requires you to list namespaces in advance. Consider the following SOAP-XML snippet:

Code Block
<c<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <c   <s:Body>
       <GetClient xmlns="http://www.srpcs.com">
           <clientId xmlns="">1</clientId>
       </GetClient>
   </cs:Body>
</cs:Envelope>

Notice the namespace is set three times. The SOAP namespace is set to 's', and the default namespace is set to the SRP Computer Solutions, Inc. website. The default namespace is changed again later to null. Without setting namespaces in advance, the following query will fail:

Code Block
XPath = "/cs:Envelope/s:Body/GetClient/clientId/text()" 
Result = SRP_Extract_Xml(Xml, XPath)

The 's' namespace, while embedded in the XML itself, is not recognized by the XPath query. We have to specify the namespace in advance. We also have to specify the default namespace. So, it would seem that our query should be (note the space between each namespace):

Code Block
XPath = "/cs:Envelope/s:Body/GetClient/clientId/text()" 
NameSpaces  = "xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' "
NameSpaces := "xmlns='http://www.srpcs.com'" 
Result = SRP_Extract_Xml(Xml, XPath, NameSpaces )

However, this will fail also. That is because XPath gets confused with the default namespace being changed within the XML. The following query does work:

Code Block
XPath = "/cs:Envelope/s:Body/srp:GetClient/clientId/text()" 
NameSpaces  = "xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' "
NameSpaces := "xmlns:srp='http://www.srpcs.com'" 
Result = SRP_Extract_Xml(Xml, XPath, NameSpaces )

...