cos2912
7 years agoNew Contributor
How to get the entire subtree from a xml response with xpath
How can I get a section of a REST response in xml to use in a REST request.
I use the Property Transfer step with Property=ResponseAsXml and Path language=XPath.
What xpath should I write if I want to get all the highlighted xml in the example
<a>
<b k="xxx">
<c l="yyy">
<d>4567</d>
<e>jjj</e>
<f>100</f>
</c>
</b>
<b k="zzz">
<c l="rrr">
<d>6689</d>
<e>ggg</e>
<f>200</f>
</c>
</b>
<b k="hhh">
<c l="www">
<d>2245</d>
<e>juk</e>
<f>300</f>
</c>
</b>
</a>
No, the ?. is real Groovy code, you don't need to change it.
You will want to do something like this Groovy script, which acts like a Property Transfer step.
import groovy.xml.StreamingMarkupBuilder
String xml = testRunner.testCase.getTestStepByName('someEarlierTestStep')
.httpRequest.response.responseContent
String innerXml = new XmlSlurper().parseText(xml)
.'b'
?.find { b -> b.'@k' == 'zzz' }
?.with { node -> new StreamingMarkupBuilder().bindNode(node) } as String
testRunner.testCase.setPropertyValue("InnerXML", innerXml)