XML : Getting an attribute value using Perl XPath

I'm trying to extract the attribute "id" from messaggioUtente on this XML code:

  <?xml version="1.0" encoding="UTF-8" ?>  <?meta name="GENERATOR" content="XML::Smart/1.78 Perl/5.022001 [MSWin32]" ?>    <messaggiUtenti schemaLocation="messagiUtentiSchema.xsd">    <messaggioUtente id="1">      <nome>Prova Evento</nome>      <email>example@email.com</email>      <sitoweb>www.example.com</sitoweb>      <messaggio>Sample</messaggio>    </messaggioUtente>  </messaggiUtenti>    

My idea was to use XML::XPath and XML::XPath::XMLParser in this way, but i'm getting an incorrect result:

  my $xp = XML::XPath->new(filename => 'newfile.xml');  my $nodeset = $xp->find('//@id');     foreach my $node ($nodeset->get_nodelist) {            print XML::XPath::XMLParser::as_string($node);   }    

The problem is, i'm trying to get the integer value from the id, while this code extract the whole string id = "1".

What's your suggestion to achieve this? My goal is getting the id number and increase it till i get a new unused id for my next messaggioUtente value. So my code is something like this, but due to the string problem it's not correct.

  $id = 1;    my $xp = XML::XPath->new(filename => 'newfile.xml');  my $nodeset = $xp->find('//@id');     foreach my $node ($nodeset->get_nodelist) {            my $tempvar = XML::XPath::XMLParser::as_string($node);          if($node eq $id)          {              $id = $id + 1;          }       }    

No comments:

Post a Comment