in reply to Re: How to retrieve the last node value
in thread Retrieving the last node value from an XML file

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: How to retrieve the last node value
by gellyfish (Monsignor) on Aug 15, 2006 at 13:12 UTC

    Right, I think, from piecing together the clues from all the other posts I have worked out what you want to do. You want to add a new <LIST /> with the next values of NICKID and ROWID so you need to find the greatest existing values. This then should do it:

    use XML::XPath; my $file = 'memlist.xml'; my $xp = XML::XPath->new(filename=>$file); my $nodeset = $xp->find('/MEMBERLIST/LIST'); + my $max_nickid = -1; my $max_rowid = -1; + foreach my $list ( $nodeset->get_nodelist) { my $nickid = $list->findvalue('NICKID/text()')->value(); my $rowid = $list->findvalue('ROWID/text()')->value(); + $max_nickid = $nickid if ($nickid > $max_nickid); $max_rowid = $rowid if ($rowid > $max_rowid); } + print "NICK: $max_nickid ROWID: $max_rowid\n";

    On a more general note I think you might be better served if you explain what it is you are trying to do in a wider sense in future as you seem to be posting lots of little questions dealing with a very small part of your larger problem which makes it difficult for people to help you, it also seems to indicate that you don't have a clear design of what you are trying to achieve when you start out, explaining the bigger picture to us will almost certainly help you get a view of what you are trying to achieve as well as making it possible for us to make suggestions as to how to achieve what you want rather than nibbling away at the problem.

    /J\

Re^3: How to retrieve the last node value
by derby (Abbot) on Aug 15, 2006 at 12:07 UTC

    Greatest value of what? NICK?, ROWID?, RANK?

    If you just want to get the last node in the list:

    #!/usr/local/bin/perl use XML::XPath; my $file = 'test.xml'; my $xp = XML::XPath->new(filename=>$file); my $nodeset = $xp->find('/MEMBERLIST/LIST[position()=last()]'); foreach my $node ($nodeset->get_nodelist) { print "FOUND\n\n", XML::XPath::XMLParser::as_string($node), "\n\n"; }

    If you want the greatest value of a particular field then you're going to have to sort by that field first. I think that's the point where you run into limitations with XML::XPath (and I would recommend a combination of XML::LibXML and XML::LibXSLT.

    -derby
    A reply falls below the community's threshold of quality. You may see it by logging in.