Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello mirod.

I read your post and tried. And found it seems not working good. This prints

And the script is like this. Just added example xml with your script.
#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $xml=join('',<DATA>); my $t=XML::Twig->new( start_tag_handlers => { _all_ => \&store_line_number, }, twig_handlers => { _all_ => \&warn_on_empty_elt, }, ); $t->parse($xml); sub store_line_number { my( $twig, $elt)= @_; $elt->set_att( '#line' => $twig->current_line); $elt->parent->set_att( '#not_empty') if $elt->parent; } sub warn_on_empty_elt { my( $twig, $elt)= @_; if( ! $elt->att( '#not_empty') && $elt->text !~ m{\S}) { print $elt->att( '#line'), "\n"; } $twig->purge; } __DATA__ <gibsonca> <abc>fds </abc> <!-- ok --> <ddd></ddd> <!-- not ok --> <eee> </eee> <!-- not ok --> </gibsonca>
As you see the DATA, empty tag will be ddd, and eee(line 3,4). And print out of "1" means "gibsonca" tag. I wonder this has relation with parsing of twig, as document says,

Remember that element handlers are called when the element is CLOSED, so if you have handlers for nested elements the inner handlers will be called first.

So, as a result of purging inner elements, gibsonca tag is empty for twig, I guess.

If I comment out purge, it prints line number 3 and 4.


update: Large XML files may have some cluster that may be easy to purge. For example item tag in the below case. Maybe this will print line number of end tag(not as correct as yours), but I would like to purge like this.
#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $xml=join('',<DATA>); my $t=XML::Twig->new( twig_handlers => { '/gibsonca/item//*' => \&warn_on_empty_elt, #all descendants o +f item 'item' => sub { $_[0]->purge; }, #purge if it is item tag }, ); $t->parse($xml); sub warn_on_empty_elt { my( $twig, $elt)= @_; if ($elt->children_trimmed_text eq ''){ printf "empty tag gi=%s,line col=%s,%s\n", $elt->gi, $twig->current_line,$twig->current_column; } } __DATA__ <gibsonca> <item> <abc>fds <test> </test></abc> <!-- test not ok --> <ddd></ddd> <!-- not ok --> <eee> </eee> <!-- not ok --> </item> <item> </item> </gibsonca>

In reply to Re^2: XML Newbie by remiah
in thread XML Newbie by gibsonca

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and one hand claps...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2018-06-24 13:28 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.