in reply to XML::twig counting elements that don't have a certain attribute/value on them
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
|
|---|
| 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 |