Question

I have an XML document that looks like this:

<element>
 <sub-element></sub-element>
 <sub-element></sub-element>
 <sub-element id="add_parent"></sub-element>
</element>

and I would like to add a parent to the node having the attribute "add_parent" so it would look like this:

<element>
 <sub-element></sub-element>
 <sub-element></sub-element>
 <new-parent>
  <sub-element id="add_parent"></sub-element>
 </new-parent>
</element>

I am using XML::Twig to select the correct element like this:

#!/usr/bin/perl -w
use warnings;
use XML::Twig;

$t->parsefile ('input.xml');
$v = $t->first_elt('[@id]');

which works fine and I would like to know if it is possible, to encapsulate the select element in a new element?

Was it helpful?

Solution

yes, use the wrap_in method:

$v->wrap_in( 'new-parent');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top