in reply to Finding node with attribute XML::LibXML

This is not an LibXML solution, but I thought I'd paste it in cases anyone prefers using more perlish syntax for munging XML.
use strict; use warnings; use XML::TreeBuilder; my $doc = XML::TreeBuilder->new(); $doc->parse_file("minimal.xml"); my @vol = $doc->look_down(_tag => 'Volume', VolumeCategory => 'L'); # also tried various quoting scheme... my $tmp = scalar(@vol); print "Number of entries: $tmp \n"; # 1

Replies are listed 'Best First'.
Re^2: Finding node with attribute XML::Twig
by Discipulus (Canon) on Oct 01, 2014 at 08:03 UTC
    ..and this a XML::Twig solution.. ;=)
    use warnings; use strict; use XML::Twig; my $xml=<<'XML'; <root> <ProductKey>99</ProductKey> <Volume VolumeCategory="L" MeasurementCategory="Real">0.063</Volume> <Volume VolumeCategory="L" MeasurementCategory="Real">0.098989</Volume +> <Volume VolumeCategory="cuft" MeasurementCategory="Real">2.2</Volume> </root> XML my @vol_cat_L; my $twig= new XML::Twig( twig_handlers => { '/root/Volume[@VolumeCate +gory="L"]' => sub {push @vol_cat_L, $_}, }, ); $twig->parse( $xml); print "Number of L volume category: ",scalar @vol_cat_L,"\n"; ##OUTPUT (note the line in example data added by me): Number of L volume category: 2

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.