soapUI XPath 断言中的默认名称空间

Default namespaces in soapUI XPath assertions

我正尝试在 soapUI 中针对以下 XML 创建 XPath 匹配断言以查看元素是否存在:

<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>

我的第一次尝试是 boolean(//address),soapUI 说这是错误的。但是,当我让 soapUI 为我计算出命名空间时,它得出了:

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)

这行得通,但我不明白 ns1 名称空间从何而来。我认为这是默认设置,但如果没有 soapUI,我怎么能解决这个问题呢?

nsX 是 SoapUI 从响应生成的默认命名空间。

要计算 xPath 表达式,您需要一个能够 运行 xPath 函数的编辑器。 试试这个在线解析器:

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

这些函数 (API) 也可用于 Java:

等程序语言

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

XML 文档中的名称空间前缀不必与您的断言中的名称空间前缀相同。必须匹配的是 URI。在默认名称空间的情况下,URI 仍然必须匹配,因此您必须在 XPath/XQuery.

中调用它 something

在您的 XML 文档中,<address> 元素位于 http://namf09.anzlic.org.au 命名空间中,它指的是国家地址管理框架。

为了给那个 URI 一个有意义的名字,而不是像 ns1 这样的纯粹随机的名字,我们称它为 namf:

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