Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: XML::twig counting elements that don't have a certain attribute/value on them

by toolic (Bishop)
on Feb 10, 2010 at 16:15 UTC ( [id://822454]=note: print w/replies, xml ) Need Help??


in reply to XML::twig counting elements that don't have a certain attribute/value on them

Try this:
use strict; use warnings; use XML::Twig; my $xmlStr = <<XML; <foo> <qp> <q supp="yes">hello</q> <q supp="no">bye</q> <xr supp="yes">later</xr> </qp> </foo> XML my $twig= XML::Twig->new(); $twig->parse($xmlStr); my $elt = $twig->root(); my @qps = $elt->children('qp'); my $numOfQuotes = 0; for my $qp (@qps) { for my $q ($qp->children( qr/^(q|xr)$/ )) { $numOfQuotes++ if $q->att('supp') eq 'yes'; } } print "numOfQuotes = $numOfQuotes\n"; __END__ numOfQuotes = 2
  • Comment on Re: XML::twig counting elements that don't have a certain attribute/value on them
  • Download Code

Replies are listed 'Best First'.
Re^2: XML::twig counting elements that don't have a certain attribute/value on them
by mertserger (Curate) on Mar 05, 2010 at 15:47 UTC

    Toolic

    I have modified your suggestion (I needed the ones that weren't suppressed) as this seemed to cope with the added requirements for testing for other attributes and values. Thansk for this suggestion

    My code is something like this:

    my $numOfQuots = 0; if ( my @qps = $elt->children('qp') ) { foreach my $qp (@qps) { foreach my $q ($qp->children( qr/^(q|xr)$/ ) ) { $numOfQuots++ if (($q->att('supp') ne 'yes') && ($q->att('info') ne 'yes') && ($q->att('info') ne 'info') && ($q->att('info') ne 'intro') && ($q->att('style') ne 'IMPL')); } } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found