Monks, I have some problem in my coding.

I have the following xml as a input and in that i just want to change the 'title' element with 'tit' everywhere except second sub level title, in the below example it is the title that contains 'second sub level', which should not be changed.

For the above job i tried using ignore_elts method but it converts all the title into tit and when i saw the output it is missing, but i just want the text back in the ouput as i shown below. How to do this?

use strict; use XML::Twig; my $sel = 'title'; my $ign = 'title/title/title'; my $twig = XML::Twig->new( ignore_elts => { "$ign" => 1}, twig_handlers => { "$sel" => \&title}, pretty_print => 'indented' ); $twig->parsefile('1.xml'); $twig->print; sub title { my ($twig, $tit) = @_; $tit->set_gi('tit'); }
input: <?xml version="1.0"?> <!DOCTYPE stats SYSTEM "stats.dtd"> <stats> <title id="1">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name> +<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti +tle> <title id="2">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name> +<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti +tle> <title id="3"> <name1>Raja m</name1><g>49</g> <title> testing the sub title in title </title> <title> this is pcdata in subtitle <title> second sub level </title> </title> </title> </stats>
output: <?xml version="1.0"?> <!DOCTYPE stats SYSTEM "stats.dtd"> <stats> <tit id="1">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name> +<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti +t> <tit id="2">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name> +<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti +t> <tit id="3"> <name1>Raja m</name1> <g>49</g> <tit> testing the sub title in title </tit> <tit> this is pcdata in subtitle </tit> </tit> </stats>
Expected output: <?xml version="1.0"?> <!DOCTYPE stats SYSTEM "stats.dtd"> <stats> <tit id="1">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name> +<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti +t> <tit id="2">testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name> +<g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></ti +t> <tit id="3"> <name1>Raja m</name1> <g>49</g> <tit> testing the sub title in title </tit> <tit> this is pcdata in subtitle <title> second sub level </title> </tit> </tit> </stats>

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)


In reply to XML::Twig ignore_elts by anniyan

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.