in reply to Re^2: Help with XML::XPath
in thread Help with XML::XPath

If you insist on using XPath, here's a version. But in this case, I'd say using something like XML::Simple is simpler.
use strict; use XML::XPath; my $month = 1; my $min = 30; my $match = findmax($month,$min); foreach my $t(@$match){ print "$t->[0],$t->[1]\n"; } sub findmax{ my $month = shift; my $min = shift; $month = "-$month-"; my @result = (); my $xp = XML::XPath->new(filename => 'test.xml'); my $nodes = $xp->findnodes("//DailyWeatherRecord[contains(date,'$m +onth') and temperature/maxdrybulb[\@number>$min]"); foreach my $node($nodes->get_nodelist){ push @result,[$xp->findvalue("./date",$node),$xp->findvalue(". +//maxdrybulb/\@number",$node)]; } return \@result; } __END__ 1-1-2004,33.95

Replies are listed 'Best First'.
Re^4: Help with XML::XPath
by perl_seeker (Scribe) on Oct 31, 2004 at 08:36 UTC
    Hello Johnny!
    I still cannot do much with the module. Maybe I'm asking for too much, but could you please help me with this?(thanks for your patience!):

    I need to write a subroutine that takes as input the month number e.g. 2 for February, and searches the XML file for the monthly weather record for February, and any month
    before it i.e. January.I need to extract the total rainfall amounts from these records and find their sum.

    The subroutine returns the month number and the sum in an array.If I needed to look for only the February monthly record or $month='2', I guess the code would look like this:
    sub RainSoFarMessage{ my $month = shift; $pattern = "$month-"; my $xp = XML::XPath->new(filename => 'weather_records.xml'); my @result = (); my @totrain=(); my $nodes = $xp->findnodes("//MonthlyWeatherRecord [contains(date,'$pattern')]"); foreach my $node($nodes->get_nodelist){ push @totrain,$xp->findvalue(".//totalrainfall/ \@number",$node); } my $total=0; foreach my $t(@totrain){ $total=$total + $t; print "\n$total"; } push @result [$month,$total]; return \@result; }
    I know this seems silly, but how can I set the value of '$pattern' so that the search pattern includes the month passed as a parameter to the sub, as well as all months before it, starting from Jan? Please help!

    I also need to write another sub, which also takes the month number as input, and searches the daily weather records for that month and extracts the maxdrybulb values . I need to search for each set of 5 (for the time being say, better to make this a variable that can be set) consecutive days where the maxdrybulb values fall in the same range( 'temperature spells' of the month).
    The temperature ranges could be defined as for example:
    Over 40.0 extremelyhot 35.0 to 39.9 veryhot 30.0 to 34.9 hot 25.0 to 29.9 verywarm
    I need to then build an array of arrays, where each array corresponds to a temperature spell (5 consecutive days with max. temp in the same range) and stores the start date, end date, and the string representing the temp. range('hot','verywarm', etc.). For e.g.,
    1-2-2004 5-2-2004 'verywarm'
    The sub returns this array of arrays to main. Of course right now the xml file above has only 2 daily weather records for each month.My monthly weather records now look like this, with rainfall info added.
    <MonthlyWeatherRecord> <date>1-2004</date> - <temperature> <maxdrybulb unit="degrees-centigrade" number="33.95" /> <mindrybulb unit="degrees-centigrade" number="23.20" /> <avgdrybulb unit="degrees-centigrade" number="33.95" /> </temperature> <totalrainfall unit="mm" number="0.5" /> </MonthlyWeatherRecord>
    All this seems really complicated. I do have some code, but not much. Please help!
    Thanks,
    perl_seeker:)
Re^4: Help with XML::XPath
by perl_seeker (Scribe) on Aug 30, 2004 at 08:06 UTC
    Hello,
    thanks a lot, this really helps. Could you suggest a good tutorial for Xpath or for the perl module XML::XPath,
    so that I can start doing some things?

    Cheers!
    perl_seeker :)
      http://www.w3schools.com/ has some nice tutorials on XML in general. There may be an XPath tutorial by now. In fact there is.

      --Solo

      --
      You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.