Jackson
Jackson

Reputation: 101

How to duplicate a node and its children in Karate

Hi I was trying to replace a node in the second level of the structure, eg the "two" node below (I want to duplicate this node and its children)

<root>
   <first>1</first>
   <second>
      <one>1</one>
      <two>
         <a>a</a>
         <b>b</b>
         <c>c</c>
      </two>
      <three>c</three>
   </second>
   <third>3</third>
</root>

So that it ends up looking like this:

<root>
   <first>1</first>
   <second>
      <one>1</one>
      <two>
         <a>a</a>
         <b>b</b>
         <c>c</c>
      </two>
      <two>
         <a>a</a>
         <b>b</b>
         <c>c</c>
      </two>
      <three>c</three>
   </second>
   <third>3</third>
</root>

Is this possible to do in Karate?

Many thanks

Upvotes: 2

Views: 113

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

Normally I don't recommend tests that are "too clever" - each Scenario should be focused on one "static" payload - and you will thank yourself later when you try to read your tests.

Also make sure you look at this set of examples for "templating" ideas: xml.feature

But anyway, here you go:

* def root =
"""
<root>
   <first>1</first>
   <second>
      <one>1</one>
      <two>
         <a>a</a>
         <b>b</b>
         <c>c</c>
      </two>
      <three>c</three>
   </second>
   <third>3</third>
</root>
"""
* xmlstring two = $root/root/second/two
* replace root.<three>c</three> = two + '<three>c</three>'
* xml root = root
* print root

Upvotes: 2

Related Questions