Forum Discussion

cos2912's avatar
cos2912
New Contributor
7 years ago
Solved

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)

     

8 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    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)

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    cos2912 , it would be helpful if you specify more details of your use case and that extracted data is going to be used?
  • cos2912's avatar
    cos2912
    New Contributor

    //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's avatar
      nmrao
      Champion Level 3
      what do you get in readyapi?
  • JHunt's avatar
    JHunt
    Community Hero

    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's avatar
    cos2912
    New Contributor

    I'm ready!

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