Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl LibXML findnodes($query)_Perl_Xpath_Libxml2 - Fatal编程技术网

Perl LibXML findnodes($query)

Perl LibXML findnodes($query),perl,xpath,libxml2,Perl,Xpath,Libxml2,我在使用此代码时遇到一些问题: my $file= '../xml/news.xml'; my $parser= XML::LibXML->new(); my $doc = $parser->parse_file($file); my $xpc = XML::LibXML::XPathContext->new($doc); my $query = '/notizie/news[@id='.$newsId.']'; print $query; my $node = $xpc-&g

我在使用此代码时遇到一些问题:

my $file= '../xml/news.xml';
my $parser= XML::LibXML->new();
my $doc = $parser->parse_file($file);
my $xpc = XML::LibXML::XPathContext->new($doc);
my $query = '/notizie/news[@id='.$newsId.']';
print $query;
my $node = $xpc->findnodes($query)->get_node(1);

print $node;
特别是“print$node”打印一个空字符串,即使XML文件路径正确并且XPath查询应该返回一个节点

“有趣”的是,如果我使用:

my $query = '/*/*[@id='.$newsId.']'; 
我得到了正确的结果

这是news.xml文件:

<?xml version="1.0"?>
<notizie xmlns="http://www.9armonie.com/news"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.9armonie.com/news news.xsd">
    <news id="3">
        <data>2015-01-01</data>
        <ora>12:00:00</ora>
        <titolo>Title 3</titolo>
        <descrizione> Description 3</descrizione>
    </news>     
    <news id="2">
        <data>2014-12-19</data>
        <ora>12:00:00</ora>
        <titolo>Title 2</titolo>
        <descrizione> Description 2</descrizione>
    </news>
    <news id="1">
        <data>2014-12-18</data>
        <ora>12:00:00</ora>
        <titolo>News 1</titolo>
        <descrizione> Desc 1</descrizione>
    </news>
    <news id="0">
        <data>2014-12-18</data>
        <ora>12:00:00</ora>
        <titolo> asdasd</titolo>
        <descrizione> First! </descrizione>
    </news>
</notizie>

2015-01-01
12:00:00
标题3
说明3
2014-12-19
12:00:00
标题2
说明2
2014-12-18
12:00:00
新闻1
描述1
2014-12-18
12:00:00
asdasd
第一!

您的输入XML文档位于默认名称空间中:

忽略名称空间

第二个选项意味着将XPath表达式修改为

"/*[local-name() = 'notizie']/*[local-name() = 'news' and @id='.$newsId.']"
上述表达式将在以下所有文档中找到
notizie
元素:

<!--No namespace-->
<notizie/>

<!--Namespace with prefix-->
<news:notizie xmlns:news="http://www.9armonie.com/news"/>


<!--Default namespace-->
<notizie xmlns="http://www.9armonie.com/news"/>

如何填充
$newId
?另外-您是否使用了
严格
警告
"/*[local-name() = 'notizie']/*[local-name() = 'news' and @id='.$newsId.']"
<!--No namespace-->
<notizie/>

<!--Namespace with prefix-->
<news:notizie xmlns:news="http://www.9armonie.com/news"/>


<!--Default namespace-->
<notizie xmlns="http://www.9armonie.com/news"/>