Ask a Question

How to get the entire subtree from a xml response with xpath

SOLVED
cos2912
New 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>

1 ACCEPTED SOLUTION

Accepted Solutions
JHunt
Community Hero

Re: How to get the entire subtree from a xml response with xpath

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)

 

View solution in original post

8 REPLIES 8
nmrao
Community Hero

Re: How to get the entire subtree from a xml response with xpath

@cos2912 , it would be helpful if you specify more details of your use case and that extracted data is going to be used?


Regards,
Rao.
ranjit96
Contributor

Re: How to get the entire subtree from a xml response with xpath

nmrao
Community Hero

Re: How to get the entire subtree from a xml response with xpath

Selection_385.png

 

Required xpath is : //b[@k='zzz']



Regards,
Rao.
cos2912
New Contributor

Re: How to get the entire subtree from a xml response with xpath

//b[@k='zzz'] That works when I use another xpath evaluater, but in ReadyAPI it doesn't work. That's my problem in a nutshell, what works in a xpath evaluater doesn't work in ReadyAPI, so maybe ReadyAPI uses a primitive version of xpath.

nmrao
Community Hero

Re: How to get the entire subtree from a xml response with xpath

what do you get in readyapi?


Regards,
Rao.
JHunt
Community Hero

Re: How to get the entire subtree from a xml response with xpath

If you're open to giving up on XPath, try this:

 

import groovy.xml.StreamingMarkupBuilder

String innerXml = new XmlSlurper().parseText(xml)
 .'b'
 ?.find { b -> b.'@k' == 'zzz' }
 ?.with { node -> new StreamingMarkupBuilder().bindNode(node) } as String

assert innerXml == "<b k='zzz'><c l='rrr'><d>6689</d><e>ggg</e><f>200</f></c></b>"

(I used this article to construct my answer)

cos2912
New Contributor

Re: How to get the entire subtree from a xml response with xpath

I'm ready!

But how do I point out the xml response from a REST step? I assume the "?" stands for that.

JHunt
Community Hero

Re: How to get the entire subtree from a xml response with xpath

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)

 

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: