XML::LibXML::Reader 给出错误的匹配元素
这是我使用 XML::LibXML::Reader 解析的 XML
<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>
,这是代码
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 给我两个元素。为什么?只有一个世界标签。我的代码有什么问题吗?
同样,当我使用该模式时,
my $pattern = XML::LibXML::Pattern->new('/world/country');
它给了我四个元素,而我只有两个国家/地区标签。
请解释一下我哪里做错了? 我需要使用 Pattern(用于 xPath)并且我无法避免它。另外,出于某些兼容性原因,我想坚持使用 XML::LibXML::Reader。
请帮忙。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
交叉发布于 PerlMonks。谦虚的我在那里回答了。
Crossposted at PerlMonks. Answered there by humble me.