Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: XML::Twig removing tags from content (XML::Twig)

by toolic (Chancellor)
on Sep 26, 2011 at 17:07 UTC ( #927902=note: print w/ replies, xml ) Need Help??


in reply to XML::Twig removing tags from content

cut_children removes the 'p' tags and gets you closer...

use warnings; use strict; use XML::Twig; my $str = <<EOF; <li> <p> Some <b>text</b> </p> </li> EOF my $t = XML::Twig->new( twig_handlers => {li => \&li}, pretty_print => 'indented', ); $t->parse($str); $t->print(); sub li { my ($t, $elt) = @_; for my $p ($elt->children('p')) { for my $c ($p->cut_children()) { $c->paste($elt); } $p->delete(); } } __END__ <li><b>text</b> Some </li>


Comment on Re: XML::Twig removing tags from content (XML::Twig)
Select or Download Code
Re^2: XML::Twig removing tags from content (XML::Twig)
by slugger415 (Beadle) on Sep 26, 2011 at 19:52 UTC

    Hi Canon, getting closer, but for some reason the children are coming out in the wrong order (reversed?):

    Original:

    <li class="c2">
    	<p class="Number1">In the XYZ pane, select the <span class="c1">PageID</span> ruleset and click the <span class="c1">Lock/Unlock ruleset</span> button. Then expand the
    	<span class="c1">PageID</span> ruleset to view the two rules.</p>
    </li>
    
    

    Result:

          <li class="c2"> ruleset to view the two rules.<span class="c1">PageID</span> button. Then expand the
                <span class="c1">Lock/Unlock ruleset</span> ruleset and click the <span class="c1">PageID</span>In the XYZ pane, select the </li>
    

    Thoughts?

      To answer my own question, using reverse seems to fix the problem:

      for my $p ($elt->children('p')) { for my $c (reverse($p->cut_children())) { $c->paste($elt); } $p->delete(); }

      I don't understand why it's needed, but it works.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://927902]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-12-22 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls