in reply to Re^3: xml::twig delete element
in thread xml::twig delete element

Hi mirod, please find my code below. I am using the below code in process of "content" element and find "$fcnvar" Which is from "box" element not equal to blank then i need to search '//color@self="ua5"' and i should get the attribute "pnam" of that "color" element. how can i do that. can you please suggest. i am struggling here.
use strict; use warnings; use XML::Twig; my $twig=XML::Twig->new( twig_handlers=>{ content=> \&content_handler, }, ); $twig->parse(\*DATA); $twig->flush; sub content_handler { my ($twig, $content)= @_; if($content->{'att'}->{'type'} eq "text") { my $selfatt=$content->att_xml_string('Self'); my $expr='//box[@textflowid="'.$selfatt.'"][@type="text"]'; my @tagnames = $twig->get_xpath($expr); for my $temp (@tagnames) { my $fcnvar=$temp->att_xml_string('frameColorID'); # print "#$fcnvar#"; # if (!($fcnvar eq "")){ # my $expr='//color[@self="ua5"]'; # print "$expr"; # my @colorvalarr = $twig->get_xpath($expr); # print @colorvalarr; # print $colorvalarr[0]; # $fcnvar=$colorvalarr[0]->att_xml_string('pnam'); # } $content->set_tag('updated_tag'); $content->del_atts; $content->set_atts } } } __DATA__ <document> <root> <box type="text" cont="text" textflowid="rc_u5035" frameColorID="ua +5"/> </root> <content type="text" Self="rc_u5035" id="rc_u5035"/> <header>h3</header> <meta> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_15_D_50_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 04-15M/50Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="uce"/> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_10_D_40_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 05-10M/40Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="ua5"/> </meta> </document>
OUTPUT REQUIRED:
<document> <root> <box type="text" cont="text" textflowid="rc_u5035" frameColorID="ua +5"/> </root> <updated_tag color="Color 05-10M/40Y"/> <header>h3</header> <meta> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_15_D_50_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 04-15M/50Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="uce"/> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_10_D_40_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 05-10M/40Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="ua5"/> </meta> </document>

Replies are listed 'Best First'.
Re^5: xml::twig delete element
by mirod (Canon) on Oct 13, 2009 at 11:53 UTC

    This should do more or less what you want:

    use strict; use warnings; use XML::Twig; my %self2pnam; # Self => pnam for all color elements my $twig=XML::Twig->new( # create self2pnam twig_handlers=>{ color => sub { $self2pnam{$_- +>att('Self')}= $_->att( 'pnam'); } }, pretty_print => 'indented', ); $twig->parse(\*DATA); # now we can process the content elements foreach my $content ($twig->root->children( 'content')) { my $box= $content->prev_elt( 'box[@type="text"]'); $content->set_tag( 'updated_tag') ->del_atts ->set_att( Color => $self2pnam{$box->att( 'frameColorID')} +) ; } $twig->print; __DATA__ <document> <root> <box type="text" cont="text" textflowid="rc_u5035" frameColorID="ua +5"/> </root> <content type="text" Self="rc_u5035" id="rc_u5035"/> <header>h3</header> <meta> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_15_D_50_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 04-15M/50Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="uce"/> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_10_D_40_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 05-10M/40Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="ua5"/> </meta> </document>
      Thanks for the reply mirod, Is it possible from my method to do like that. i need to get and update the same where i have commented. i need to do like this process a lot with "content" elements. so it would be better i should twig the content and from there i should get whatever i want instead seperate process. it will help me in understand the process also. Please help me.

        It would be very similar: 1 pass to build the %self2pnam map, same as in the code above, except maybe adding a $_->purge at the end of the handler. The second pass would have a handler on content that would do exactly what's in the foreach loop.

        At this point, I believe you have all the elements to write the code yourself.