Get attribute values with xsi in XML node with Xpath query in SoapUI using Groovy

Viewed 517

Please find below the XML I am using, and I need to extract status of mobileNumberVerified in for my assertion validation

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

I tried xpath query "//soapenv:Envelope[1]/soapenv:Body[1]/*:AccountVerifyResponse/*:mobileNumberVerified [1]/@*" - I received "+91" which is as expected

and for xpath query "//soapenv:Envelope[1]/soapenv:Body[1]/*:AccountVerifyResponse/*:mobileNumberVerified [1]/@xsi:nil" - I got "path parser exception" error

1 Answers

You're probaly making a mistake by not setting the namespace, examples of XPath and XQuery to get the attribute value and the tag value are given below

To get the attribute value

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

Script Assertion :

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

To get the node value

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

Script Assertion :

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

You could store the value into a property using groovy or you could also use PropertyTranser

declare namespace ns1='myservice url';
//ns1:AccountVerifyResponse/ns1:mobileNumberVerified/text()

enter image description here

XQuery Match :

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