Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

XML::Twig ignore_elts

by anniyan (Monk)
on Nov 19, 2005 at 05:06 UTC ( [id://510038]=perlquestion: print w/replies, xml ) Need Help??

anniyan has asked for the wisdom of the Perl Monks concerning the following question:

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)

Replies are listed 'Best First'.
Re: XML::Twig ignore_elts
by Tanktalus (Canon) on Nov 19, 2005 at 06:15 UTC

    The ignore_elt parameter doesn't do what you think it does. It tells XML::Twig to pretend that element doesn't even exist. So what you want is to remove that, and then inside the title function, check where you're at. One way is this:

    my $twig = XML::Twig->new( twig_handlers => { $sel => \&title}, pretty_print => 'indented' ); # ... sub title { my ($twig, $tit) = @_; $tit->set_gi('tit') unless $tit->level('title') == 2; }
    This means XML::Twig will read and parse the title/title/title elements, but when we get there, we check how many levels of title's there are above us, and if that is 2 (that means we're the 3rd), then we don't set_gi.

    Good luck.

Re: XML::Twig ignore_elts
by mirod (Canon) on Nov 19, 2005 at 06:27 UTC

    As Tanktalus mentioned, you cannot use ignore_elt for this.

    An other way to do this is to set a twig handler on 'title/title/title' that will just return 0, this will prevent the handler on title to be run:

    my $twig = XML::Twig->new( twig_handlers => { $ign => sub { return 0}, $sel => \&title, },

      mirod that works fine. thank you. Tanktalus thanks for your contribution as well.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://510038]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found