rpg
rpg

Reputation: 1652

XML::LibXML::Reader giving wrong matched element

Here is my XML which I am parsing using XML::LibXML::Reader

<world>
    <country short="usa" name="united state of america">
        <state short="CA" name="california"/>
            <city short="SFO" name="San Franscisco"/>
            <city short="EM" name="Emeryville"/>
        <state short="FL" name="florida"/>
            <city .../>
            .
            <city ../>
    </country>
    <country short="abc" name="a for apple">
        <state ..../>
    </country>
</world>

and here is the code

use XML::LibXML::Reader;

my $reader = XML::LibXML::Reader->new(location => "map.xml");
my $pattern = XML::LibXML::Pattern->new('/world');

my @matchedNodes;

while ($reader->nextPatternMatch($pattern))
{
   push @matchedNodes, $reader->copyCurrentNode(1);
}

@matchedNodes give me two elements. why? There is only one world tag. What is wrong with my code?

similarly when I use the pattern

my $pattern = XML::LibXML::Pattern->new('/world/country');

It give me four elements whereas I am having only two country tags.

Please explain me where am I doing wrong? I need to use Pattern (for xPath) and I can not avoid it. Also, I would like to stick with XML::LibXML::Reader for some comtability reasons.

Please help.

Upvotes: 1

Views: 354

Answers (1)

choroba
choroba

Reputation: 241918

Crossposted at PerlMonks. Answered there by humble me.

Upvotes: 2

Related Questions