in reply to Help with XML::XPath

If I understand you correctly, the following is what you want:
use strict; use XML::XPath; my $date = "2-2004"; my @values = findmax($date); print join(",",@values),"\n"; sub findmax{ my $date = shift; my $xp = XML::XPath->new(filename => 'test.xml'); my $max = $xp->findvalue("/AnnualWeatherRecord/MonthlyWeatherRecor +d[./date=\"$date\"]/temperature/maxdrybulb/\@number"); return ($date,$max); } __END__ 2-2004, 50.25

Replies are listed 'Best First'.
Re^2: Help with XML::XPath
by perl_seeker (Scribe) on Aug 20, 2004 at 04:20 UTC
    Hello johnny!
    thanks a lot, this really helps. I needed to look in the daily weather records and not in the monthly
    one's, but that meant a very minor change in the code, and I'm getting the results I need.

    I'll have to come back here incase I need more help:)

    *Thanks to all the others for their code too.*

    Cheers
    perl_seeker:)
Re^2: Help with XML::XPath
by perl_seeker (Scribe) on Aug 25, 2004 at 05:24 UTC
    Hello,
    would you be able to tell me how to code this, still not able to use XML::XPath much:

    I need to search the XML file in concern for all the DailyWeatherRecords for a particular month.

    Say for the month of January, I need to search for those DailyWeatherRecords which have a "1" in the middle of the
    date attribute value, for example "1-1-2004" and "2-1-2004".I'm not sure how to look in the middle of the date value.

    The input to the sub that does this is the month i.e January or "1". For all the DailyWeatherRecords for the month of
    January, I need to extract the value of maxdrybulb if it is greater than 30.0, and the date. The sub should
    return as an array the list of dates and maxdrybulb values (only if it is greater than 30.0)

    For example the array might look like:
    1-1-2004 33.95 2-1-2004 30.95
    Thanks in advance.

    perl_seeker
    :)
      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
        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:)
        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 :)