This is tricky: mark, subs_text and the likes only work for text elements, nor for elements including sub elements. Below is a simple example that uses mark to wrap thext preceeding a tab, and then wrap_children to wrap the rest (in this case you could just go through abc children and use wrap_in, but if the XML is more complex than what you showed here then wrap_children might come in handy.
If your XML is more complex, and if for example the first element can include sub-elements, then the best solution might be to just to create a first, then go through the children and move them into first until you find a tab, then create a second, move children into second, until you hit a condition that switches back to a first.
#!/usr/bin/perl -w
use strict;
use XML::Twig;
use Test::More tests => 1;
$/="\n\n";
(my $input= <DATA>)=~ s{\\t}{\t}g; # I used \t in the DATA s
+ection to avoid problems
(my $expected_output= <DATA>)=~ s{\n\s*}{}g; # remove extra formating
my $t= XML::Twig->new( pretty_print => 'indented')
->parse( $input);
$t->root->mark( qr/^\n*(.*)\t/s, 'first'); # wrap text before a tab
$t->root->wrap_children( '<abc>' => 'second') # wrap remaining abc chi
+ldren
->strip_att( 'id'); # strip extra id added b
+y wrap_children
(my $output= $t->sprint)=~ s{\n\s*}{}g;
ok( $output eq $expected_output);
__DATA__
<ext>
abc\t<abc><p>ahgkslkjs</p>
<p>hai</p></abc>
abc\t<abc><p>ahgkslkjs</p></abc>
</ext>
<ext>
<first>abc</first>
<second>
<abc>
<p>ahgkslkjs</p>
<p>hai</p>
</abc>
</second>
<first>abc</first>
<second>
<abc>
<p>ahgkslkjs</p>
</abc>
</second>
</ext>
|