的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,它可以正常工作
有什么问题?
答案 0 :(得分:1)
"//zoo:animale[nome='".$name."']";
必须:
"//zoo:animale[zoo:nome='".$name."']";