无法通过 groovy 中的 getNodeValue 函数获取 xpath 中的节点值

not able to get node value in the xpath by getNodeValue function in groovy

我无法在以下响应中获取 "idNumber" 的节点值 xml

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <SOAP_Customer_Metadata_CALLResponse xmlns="http://BCE/Customer_Metadata_WS.tws">
         <oMetaDataID>
            <ocrStatus>Failed</ocrStatus>
            <guid>123456</guid>
            <docType>03</docType>
            <docDescription>South African ID</docDescription>
            <surname>Choudhary</surname>
            <fullName>Kanika</fullName>
            <idNumber>KANJANDHS293</idNumber>
            <dateOfBirth>22091945</dateOfBirth>
            <dateIssued>01012016</dateIssued>
            <countryOfBirth>India</countryOfBirth>
            <idType>ID</idType>
            <gender>F</gender>
         </oMetaDataID>
         <oMetaDataPOA>
            <ocrStatus>Passed</ocrStatus>
            <surname>Choudhary</surname>
            <idNo>12345</idNo>
            <address1>abc def</address1>
         </oMetaDataPOA>
         <oResubmission>No</oResubmission>
         <oCASASequenceNo>1234578</oCASASequenceNo>
         <oTypeOfCustomer>New</oTypeOfCustomer>
      </SOAP_Customer_Metadata_CALLResponse>
   </soapenv:Body>
</soapenv:Envelope>

Groovy 脚本中使用以下代码 testStep:

holder.getNodeValue("//idNumber")

这可能是因为 XML 有 默认命名空间 :

xmlns="http://BCE/Customer_Metadata_WS.tws"

SOAP_Customer_Metadata_CALLResponse 中所有没有前缀的元素,包括 idNumber,都被认为在该命名空间中。对于默认命名空间中的 select 元素,您需要将前缀映射到默认命名空间 URI,并在 XPath 中使用该前缀。例如,如果注册的前缀是 d//d:idNumber(我不知道 groovy 但 this post 可能有用)。

一种等效的纯 XPath 方法是使用 XPath 函数 local-name()namespace-uri() 的组合:

//*[local-name()='idNumber' and namespace-uri()='http://BCE/Customer_Metadata_WS.tws']

作为@har07 的回答,您可以定义一个命名空间前缀,然后在您的XPath 中使用它。然而 SOAPUI XmlHolder 支持命名空间的通配符 * 所以你可以简单地在你的 XPath:

holder.getNodeValue("//*:idNumber")

编辑

您在评论中使用 holder.getNodeValue("//*:oMetaDataID/ocrStatus") 的问题是您缺少第二个元素的名称空间,在这里您还可以再次使用 * 通配符作为:

holder.getNodeValue("//*:oMetaDataID/*:ocrStatus")