Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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

3
4
1
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.

regards.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found