perl - Understanding XML::Twig"s wrap_in -


i looping through twig's descendants, , in loop want create new twigs output later. new twigs wrapped versions of current looped item. this:

# $twig exists. @descendants = $twig->root->first_child->descendants_or_self; foreach (@descendants) {   $_->root->wrap_in('tree');    $treetop = xml::twig->new()->set_root($_);    $treetop->root->wrap_in('trees', treebank => {     id => 'someid'   });    if (exists $hash{'somekey'}) {     $treetop->root->set_att(c => 'd');   } } 

an example of $_->sprint in loop:

<node begin="0">   <node a="b"></node> </node> 

however, result of (after last if-clause) ($treetop->sprint):

<node begin="0" c="d">   <node a="b"></node> </node> 

in other words, attribute added initial 'root', , no wrapping happens. i'm trying achieve is:

<treebank id="someid" c="d">   <trees>     <tree>       <node begin="0">         <node a="b"></node>       </node>     </tree>   </trees> </treebank> 

interestingly, when call $_->root see original root ($twig's root), guess root implicitly inherited part of object. think that's of confusion lies: root of special $_ root of $twig , not root of sub tree itself.

what right way take input twig descendant, turn twig wrapping structure?

normally when trying create subdocuments that, create new one, , insert copied node.

something this:

#!/usr/bin/env perl  use strict; use warnings;  use xml::twig;  $twig = xml::twig->new->parse( \*data );  foreach $node ( $twig->get_xpath('./node') ) {     $new_root =      xml::twig::elt->new( 'treebank', { id => "someid", c => "d" } );    $new_doc = xml::twig->new->set_root($new_root);    $new_doc->set_xml_version('1.0');    $tree = $new_doc->root->insert_new_elt('trees')->insert_new_elt('tree');     $node->cut;    $node->paste( 'last_child', $tree );     $new_doc->set_pretty_print('indented');    $new_doc->print; }  __data__ <xml>  <node begin="0" c="d">    <node a="b"></node> </node> </xml> 

but address specific points - yes, root give document root. it's special case xml element, , root points @ top level, because it's part of context of node.

wrap_in special case modifying node won't work root node, because they're special case. (using example above):

foreach $node ( $twig->get_xpath('./node') ) {    $new_doc = xml::twig->new;    $new_doc->set_xml_version('1.0');     $node->cut;    $new_doc->set_root ($node);    $node->wrap_in( 'trees', treebank => { id => 'someid' } );    $new_doc->set_pretty_print('indented');    $new_doc->print; } 

you can separate out using cut , paste methods of xml::twig,


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -