1

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>[email protected]</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 until 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;
        } 
    }
3
  • I think you want $node->getNodeText Commented Jan 17, 2016 at 0:33
  • @ikegami: No such method getNodeText in XML::XPath::Node::AttributeImpl Commented Jan 17, 2016 at 4:05
  • XML::XPath::XMLParser::as_string($node) is generally written $node->toString Commented Jan 17, 2016 at 4:08

1 Answer 1

2

Each element of the nodeset is a XML::XPath::Node::Attribute object which has a getNodeValue method to get the value of the node

The best way to get the next ID in sequence is to find the maximum value of all the id attributes and add one to it

It's also probably best to use an XPath expression of //messaggioUtente/@id to avoid picking up the id attribute of any other element

This code demonstrates. I've addded two more elements to your sample data with id values of 2 and 3 to show the functionality better

use strict;
use warnings 'all';
use feature 'say';

use XML::XPath;
use List::Util 'max';

my $xp = XML::XPath->new(ioref => \*DATA);

my $ids = $xp->find('//messaggioUtente/@id');

my $new_id = 1 + max map { $_->getNodeValue } $ids->get_nodelist;

say "New ID = $new_id";

__DATA__
<?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>[email protected]</email>
    <sitoweb>www.example.com</sitoweb>
    <messaggio>Sample</messaggio>
  </messaggioUtente>
  <messaggioUtente id="2">
    <nome>Prova Evento</nome>
    <email>[email protected]</email>
    <sitoweb>www.example.com</sitoweb>
    <messaggio>Sample</messaggio>
  </messaggioUtente>
  <messaggioUtente id="3">
    <nome>Prova Evento</nome>
    <email>[email protected]</email>
    <sitoweb>www.example.com</sitoweb>
    <messaggio>Sample</messaggio>
  </messaggioUtente>
</messaggiUtenti>

output

New ID = 4
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your kindness and for explanation, i'll add your solution to my script ! I tried to look at this library's documentation but it was very unclear for me! Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.