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>

In reply to Re: XML::Twig tag conversion by mirod
in thread XML::Twig tag conversion by murugu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.