#!/bin/perl -w

#########################################################################
#                                                                       #
#  This example shows how to use the twig_roots option                  #
#  It outputs the name of the leader in a statistical category          #
#                                                                       #
#########################################################################

use strict;
use XML::Twig;

my $leader_name;
my $leader_score=0;


my $field= $ARGV[0] || 'ppg';
                                              # twig will be created only
                                              # for name and $field elements
my $twig= new XML::Twig( twig_roots    => { 'name' => 1, $field => 1 },
                                              # handler will be called for
                                              # $field elements
                         twig_handlers => { $field => \&field } ); 

$twig->parsefile( "nba.xml");                 # parse the twig
                                              # print the result
print "Leader in $field: $leader_name ($leader_score $field)\n";

sub field
  { my( $twig, $field)= @_;                      
                                              # get the score
    my $score= $field->text;    
    if( $score > $leader_score)               # if it's the highest
      { $leader_score= $score;                # store the information
        $leader_name= $field->prev_sibling( 'name')->text;
      }
    $twig->purge;                             # delete the twig so far   
 }

