In this SOAP XML file, how can I get the 7
on a using a XPath query?
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>7</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
This XPath query is not working //*[name () ='soap:Body']
.
The Default NamespaceXPath treats the empty prefix as the null namespace. In other words, only prefixes mapped to namespaces can be used in XPath queries. This means that if you want to query against a namespace in an XML document, even if it is the default namespace, you need to define a prefix for it.
When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.
If you have a namespace prefix set, you could use it, like:
//soap:Body
But since the nodes you are trying to get use a default namespace, without a prefix, using plain XPath, you can only acesss them by the local-name()
and namespace-uri()
attributes. Examples:
//*[local-name()="HelloWorldResult"]/text()
Or:
//*[local-name()="HelloWorldResult" and namespace-uri()='http://tempuri.org/']/text()
Or:
//*[local-name()="HelloWorldResponse" and namespace-uri()='http://tempuri.org/']/*[local-name()="HelloWorldResult"]/text()
To your xml, they will all give the same result, the text 7
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With