1
votes

I'm trying to create an XPath Match assertion in soapUI against the following XML to see if an element exists:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns2:executeResponse xmlns:ns2="http://ws.namf09.anzlic.org.au">
         <responses id="?" xmlns="http://namf09.anzlic.org.au">
            <result status="OK" completed="true" hasErrorsInResponseElements="false"/>
            <response id="200.1">
               <responseResult>
                  <address>
                     <streetNumber1>2</streetNumber1>
                     <streetName>sydney</streetName>
                     <streetType>SQ</streetType>
                     <localityName>sydney</localityName>
                     <stateTerritory>NSW</stateTerritory>
                     <postcode>2000</postcode>
                  </address>
               </responseResult>
            </response>
         </responses>
      </ns2:executeResponse>
   </soapenv:Body>
</soapenv:Envelope>

My first attempt was boolean(//address), which soapUI said was false. But, when I've let soapUI work out the namspaces for me, it has come up with:

declare namespace ns2='http://ws.namf09.anzlic.org.au';
declare namespace ns1='http://namf09.anzlic.org.au';
declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/';
boolean(//ns1:address)

This works, but I don't understand where the ns1 namespace has come from. I assume it's a default, but how could I work this out without soapUI?

2

2 Answers

1
votes

The namespace prefix in the XML document does not have to be the same as the namespace prefix in your assertion. It's the URI that has to match. In the case of default namespaces, the URI still has to match, so you have to call it something in XPath/XQuery.

In your XML document, the <address> element is in the http://namf09.anzlic.org.au namespace, which refers to the National Address Management Framework.

In order to give that URI a moniker that makes sense, instead of purely random one like ns1, let's call it namf:

declare namespace namf='http://namf09.anzlic.org.au';
boolean(//namf:address)
0
votes

nsX are the default namespaces that SoapUI generates from the response.

To evaluate xPath expressions you need an editor able to run xPath functions. Try this online parser:

https://www.freeformatter.com/xpath-tester.html#ad-output

These functions (APIs) are also available for program languages like Java:

What's a simple way in java to evaluate an xpath on a string and return a result string