为什么我的 XPath 表达式在 XML 文档中找不到新添加的节点?

Why doesn't my XPath expression find a newly added node in an XML document?

我尝试向 XML (svg) 文档中添加一个新节点,但是当我随后尝试使用 XPath 表达式查询它时,它没有找到新节点。

use strict;
use warnings;
use XML::LibXML;
use XML::LibXML::XPathContext;

my $parser = XML::LibXML->new();
my $svg = $parser->load_xml(string => <<'SVG');
<svg xmlns="http://www.w3.org/2000/svg" 
     xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
<g inkscape:groupmode="layer" id="old-id" inkscape:label="old-label">...</g>
</svg>
SVG

my $layer = XML::LibXML::Element->new('g');     # same result with 'svg:g'
$layer->setAttribute('inkscape:groupmode', 'layer');
$layer->setAttribute('id', 'new-id');
$layer->setAttribute('inkscape:label', 'new-label');
$svg->documentElement()->appendChild($layer);

print "Dump:\n$svg\n";

print "Xpath:\n";
my $xpc = XML::LibXML::XPathContext->new($svg);
$xpc->registerNs('svg', 'http://www.w3.org/2000/svg');
my $xpath = '//svg:g';
foreach my $node ($xpc->findnodes($xpath)) {
    print $node->getAttribute('id'), ": ", $node->getAttribute('inkscape:label'), "\n";
}

这会打印:

Dump:
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
<g inkscape:groupmode="layer" id="old-id" inkscape:label="old-label">initially</g>
<g inkscape:groupmode="layer" inkscape:label="new-label" id="new-id"/></svg>

Xpath:
old-id: old-label

当我转储整个 xml 文档时,新节点出现了,但 XPath 表达式只报告旧节点。

这是为什么?我如何获得 XPath 表达式来找到新添加的节点?[​​=13=]

虽然您的 print 语句正在输出 XML,如果重新解析,将为您提供正确的结果,但您的内存中 DOM 不知道新元素的命名空间。要告诉新元素有关名称空间的信息,我们可以根据 docs

使用 SetNamespaceSetAttributeNS
use strict;
use warnings;
use XML::LibXML;
use XML::LibXML::XPathContext;

my $parser = XML::LibXML->new();
my $svg = $parser->load_xml(string => <<'SVG');
<svg xmlns="http://www.w3.org/2000/svg" 
     xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
<g inkscape:groupmode="layer" id="old-id" inkscape:label="old-label">...</g>
</svg>
SVG

my $layer = XML::LibXML::Element->new('g');     # same result with 'svg:g'
$layer->setNamespace('http://www.w3.org/2000/svg');
$layer->setNamespace('http://www.inkscape.org/namespaces/inkscape', 'inkscape', 0);
$layer->setAttributeNS('http://www.inkscape.org/namespaces/inkscape', 'groupmode', 'layer');
$layer->setAttribute('id', 'new-id');
$layer->setAttributeNS('http://www.inkscape.org/namespaces/inkscape', 'label', 'new-label');
$svg->documentElement()->appendChild($layer);

print "Dump:\n$svg\n";

print "Xpath:\n";
my $xpc = XML::LibXML::XPathContext->new($svg);
$xpc->registerNs('svg', 'http://www.w3.org/2000/svg');
my $xpath = '//svg:g';
foreach my $node ($xpc->findnodes($xpath)) {
    print $node->getAttribute('id'), ": ", $node->getAttribute('inkscape:label'), "\n";
}

输出

Dump:
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
<g inkscape:groupmode="layer" id="old-id" inkscape:label="old-label">...</g>
<g inkscape:groupmode="layer" id="new-id" inkscape:label="new-label"/></svg>

Xpath:
old-id: old-label
new-id: new-label