qwertoyo
qwertoyo

Reputation: 1402

XPath expression returns empty nodelist using XML::LibXML::XPathContext -> findnodes()

xml:

<zoo xmlns="http://www.zoo.com" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://www.zoo.com animali.xsd">

<area id="1" posizione="nord" nome="scimmie">
    <animale>
        <nome>Gigi</nome>
        <sesso>Male</sesso>
        <eta>3</eta>
        <img>../images/Gigi.png</img>
    </animale>

    <animale>
        <nome>Gigia</nome>
        <sesso>Female</sesso>
        <eta>2</eta>
        <img>../images/Gigia.png</img>
    </animale>
</area>

<area id="2" posizione="nord" nome="giraffe">
    <animale>
        <nome>Giro</nome>
        <sesso>Male</sesso>
        <eta>6</eta>
        <img>../images/Giro.png</img>
    </animale>
</area>
<area id="3" posizione="D8" nome="Cammelli"/>
</zoo>

code:

my $parser = XML::LibXML->new;
my $doc = $parser->parse_file("../xml/animals.xml");
my $root = $doc->getDocumentElement();
my $xpc = XML::LibXML::XPathContext->new;
$xpc->registerNs('zoo', 'http://www.zoo.com');
my $xpath_exp = "//zoo:animale[nome='".$name."']";  #also tried with "Gigi" instead of $name
my $size = $xpc -> findnodes($xpath_exp, $doc)->size();

with these xml and code, I always have $size = 0

but if I try an xpath expression without the predicate, like "//zoo:animale" or "//zoo:animale/nome"

And if I try the same expression (with the predicate) on this xpath tester: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm , it works

What's the problem?

Upvotes: 2

Views: 823

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

"//zoo:animale[nome='".$name."']";

Must be:

"//zoo:animale[zoo:nome='".$name."']";  

Upvotes: 1

Related Questions