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>
代码:
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();
使用这些 xml 和代码,我总是有 $size = 0
但如果我尝试不带谓词的 xpath 表达式,例如“//zoo:animale”或“//zoo:animale/nome”
如果我在这个 xpath 测试器上尝试相同的表达式(带有谓词):http: //www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm,它可以工作
有什么问题?