Hello mirod.
I read your post and tried. And found it seems not working good. This prints
3 4 1And 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>
#!/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
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |