使用 Groovy 在 SoapUI 中使用 Xpath 查询在 XML 节点中使用 xsi 获取属性值
Get attribute values with xsi in XML node with Xpath query in SoapUI using Groovy
请在下面找到我正在使用的 XML,我需要提取 mobileNumberVerified
的状态用于我的断言验证
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<AccountVerifyResponse xmlns="myservice url">
<mobileNumberVerified xsi:nil="true" region="+91" />
</AccountVerifyResponse>
</soapenv:Body>
</soapenv:Envelope>
我尝试了 xpath 查询 "//soapenv:Envelope[1]/soapenv:Body[1]/*:AccountVerifyResponse/*:mobileNumberVerified [1]/@*"
- 我收到了预期的“+91”
对于 xpath 查询 "//soapenv:Envelope[1]/soapenv:Body[1]/*:AccountVerifyResponse/*:mobileNumberVerified [1]/@xsi:nil"
- 我收到“路径解析器异常”错误
你可能是没有设置命名空间的错误,下面给出了XPath和XQuery获取属性值和标签值的例子
获取属性值
Groovy :
import com.eviware.soapui.support.XmlHolder
def response = testRunner.testCase.getTestStepByName("Add").getPropertyValue("response")
def holder = new XmlHolder(response)
holder.declareNamespace('ns1',"myservice url")
def attr1 = holder.getNodeValue("//ns1:mobileNumberVerified/@*:nil")
def attr2 = holder.getNodeValue("//ns1:mobileNumberVerified/@region")
log.info "Value of attribute is " + attr1
log.info "Value of attribute is " + attr2
脚本断言:
import com.eviware.soapui.support.XmlHolder
def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.namespaces["ns1"]="myservice url"
def attr1 = holder.getNodeValue("//ns1:mobileNumberVerified/@*:nil")
def attr2 = holder.getNodeValue("//ns1:mobileNumberVerified/@region")
获取节点值
Groovy :
import com.eviware.soapui.support.XmlHolder
def response = testRunner.testCase.getTestStepByName("Sample").getPropertyValue("response")
def holder = new XmlHolder(response)
holder.declareNamespace('ns1',"myservice url")
def value = holder.getNodeValue('//ns1:mobileNumberVerified/text()')
log.info value
脚本断言:
import com.eviware.soapui.support.XmlHolder
def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.namespaces["ns1"]="myservice url"
def node = holder.getNodeValue("//ns1:mobileNumberVerified/text()")
//assert node==null
//assert node!=null
您可以使用 groovy 将值存储到 属性 中,或者您也可以使用 PropertyTranser
declare namespace ns1='myservice url';
//ns1:AccountVerifyResponse/ns1:mobileNumberVerified/text()
XQuery 匹配:
declare namespace ns1='myservice url';
declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope';
for $x in //ns1:AccountVerifyResponse
return
<status>{$x/ns1:mobileNumberVerified/text()}</status>
请在下面找到我正在使用的 XML,我需要提取 mobileNumberVerified
的状态用于我的断言验证
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<AccountVerifyResponse xmlns="myservice url">
<mobileNumberVerified xsi:nil="true" region="+91" />
</AccountVerifyResponse>
</soapenv:Body>
</soapenv:Envelope>
我尝试了 xpath 查询 "//soapenv:Envelope[1]/soapenv:Body[1]/*:AccountVerifyResponse/*:mobileNumberVerified [1]/@*"
- 我收到了预期的“+91”
对于 xpath 查询 "//soapenv:Envelope[1]/soapenv:Body[1]/*:AccountVerifyResponse/*:mobileNumberVerified [1]/@xsi:nil"
- 我收到“路径解析器异常”错误
你可能是没有设置命名空间的错误,下面给出了XPath和XQuery获取属性值和标签值的例子
获取属性值
Groovy :
import com.eviware.soapui.support.XmlHolder
def response = testRunner.testCase.getTestStepByName("Add").getPropertyValue("response")
def holder = new XmlHolder(response)
holder.declareNamespace('ns1',"myservice url")
def attr1 = holder.getNodeValue("//ns1:mobileNumberVerified/@*:nil")
def attr2 = holder.getNodeValue("//ns1:mobileNumberVerified/@region")
log.info "Value of attribute is " + attr1
log.info "Value of attribute is " + attr2
脚本断言:
import com.eviware.soapui.support.XmlHolder
def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.namespaces["ns1"]="myservice url"
def attr1 = holder.getNodeValue("//ns1:mobileNumberVerified/@*:nil")
def attr2 = holder.getNodeValue("//ns1:mobileNumberVerified/@region")
获取节点值
Groovy :
import com.eviware.soapui.support.XmlHolder
def response = testRunner.testCase.getTestStepByName("Sample").getPropertyValue("response")
def holder = new XmlHolder(response)
holder.declareNamespace('ns1',"myservice url")
def value = holder.getNodeValue('//ns1:mobileNumberVerified/text()')
log.info value
脚本断言:
import com.eviware.soapui.support.XmlHolder
def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.namespaces["ns1"]="myservice url"
def node = holder.getNodeValue("//ns1:mobileNumberVerified/text()")
//assert node==null
//assert node!=null
您可以使用 groovy 将值存储到 属性 中,或者您也可以使用 PropertyTranser
declare namespace ns1='myservice url';
//ns1:AccountVerifyResponse/ns1:mobileNumberVerified/text()
XQuery 匹配:
declare namespace ns1='myservice url';
declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope';
for $x in //ns1:AccountVerifyResponse
return
<status>{$x/ns1:mobileNumberVerified/text()}</status>