XML::Twig::XPath:`findnodes` 仍然抱怨我应该使用 `findnodes` 而不是 `get_xpath`

XML::Twig::XPath: `findnodes` still compains I should use `findnodes` instead of `get_xpath`

我写了一些构建 XML::Twig 树的 Perl 模块。 不幸的是 XML::Twig 的 XPath 实现似乎不完整,所以我加载了 XML::Twig::XPath.

由于 XML::Twig 的文档指出 findnodesget_xpath 相同(还有几个),我希望我可以处理 [=21= 这样的查询]原来的XML::Twig不喜欢。

第一个 运行 XML::Twig::XPath 抱怨我必须使用 getnodes 而不是 get_xpath,所以我改变了。

不幸的是,即使在更改之后,我仍然收到相同的消息:

error in xpath expression //info_string[text()] at text()
the expression is a valid XPath statement, and you are using XML::Twig::XPath, but
you are using either 'find_nodes' or 'get_xpath' where the method you likely wanted
to use is 'findnodes', which is the only one that uses the full XPath engine
 at /usr/lib/perl5/vendor_perl/5.18.2/XML/Twig.pm line 3566.

我很困惑。

有问题的语句如下所示:

return $self->_XML()->findnodes($XPath);

_XML() 方法 returns XML::Twig 树,并且 findnodes 应该查询该树。

我也曾尝试用 use XML::Twig::XPath 替换 use XML::Twig,但这并没有改变消息。

详情

由于创建数据结构的代码及其 XML 过于复杂,我只介绍 XML 输出 ($self->_XML()->print) 和数据结构(通过 Data::Dumper)在这里。

要查询的 XML 如下所示:

<MonitoringOutput id="test-id" version="0.1">
  <description>This is a test output</description>
  <exit_code>0</exit_code>
  <status_string>OK</status_string>
  <info_string>(demo): Some interesting story</info_string>
  <perf_string>L1=0.49 L2=4.7s;;;0;7 L3=0.6;@0.2:0.3</perf_string>
  <perf_data count="3">
    <sample label="L3">
      <label>L3</label>
      <value>0.6</value>
      <thresholds>
        <warn end="0.3" inverted="1" start="0.2"/>
      </thresholds>
    </sample>
    <sample label="L1">
      <label>L1</label>
      <value>0.49</value>
    </sample>
    <sample label="L2">
      <label>L2</label>
      <value unit="s">4.7</value>
      <unit>s</unit>
      <range max="7" min="0"/>
    </sample>
    <warn_labels count="0"/>
    <crit_labels count="0"/>
  </perf_data>
</MonitoringOutput>

这是我尝试制作一个可重现的最小示例:

#!/usr/bin/perl
use warnings;
use strict;

use XML::Twig;
use XML::Twig::XPath;

my $twig = XML::Twig->new();
$twig->parse('<MonitoringOutput id="test-id" version="0.1">
  <description>This is a test output</description>
  <exit_code>0</exit_code>
  <status_string>OK</status_string>
  <info_string>(demo): Some interesting story</info_string>
  <perf_string>L1=0.49 L2=4.7s;;;0;7 L3=0.6;@0.2:0.3</perf_string>
  <perf_data count="3">
    <sample label="L3">
      <label>L3</label>
      <value>0.6</value>
      <thresholds>
        <warn end="0.3" inverted="1" start="0.2"/>
      </thresholds>
    </sample>
    <sample label="L1">
      <label>L1</label>
      <value>0.49</value>
    </sample>
    <sample label="L2">
      <label>L2</label>
      <value unit="s">4.7</value>
      <unit>s</unit>
      <range max="7" min="0"/>
    </sample>
    <warn_labels count="0"/>
    <crit_labels count="0"/>
  </perf_data>
</MonitoringOutput>');
$twig->findnodes('//info_string[text()]');

当运行在调试器下调试它时(在另一台装有较新 perl 的机器上),我得到这个输出:

error in xpath expression //info_string[text()] at text()
the expression is a valid XPath statement, and you are using XML::Twig::XPath, but
you are using either 'find_nodes' or 'get_xpath' where the method you likely wanted
to use is 'findnodes', which is the only one that uses the full XPath engine
 at /usr/lib/perl5/vendor_perl/5.26.1/XML/Twig.pm line 3685.
 at /usr/lib/perl5/vendor_perl/5.26.1/XML/Twig.pm line 7122.
    XML::Twig::Elt::_croak_and_doublecheck_xpath("//info_string[text()]", "error in xpath expression //info_string[text()] at text()") called at /usr/lib/perl5/vendor_perl/5.26.1/XML/Twig.pm line 7089
    XML::Twig::Elt::_install_xpath("//info_string[text()]") called at /usr/lib/perl5/vendor_perl/5.26.1/XML/Twig.pm line 7131
    XML::Twig::Elt::get_xpath(XML::Twig::Elt=HASH(0x5591017d3da0), "//info_string[text()]") called at /usr/lib/perl5/vendor_perl/5.26.1/XML/Twig.pm line 3685
    XML::Twig::get_xpath(XML::Twig=HASH(0x55910059bb00), "//info_string[text()]") called at /tmp/t.pl line 38

似乎在构造树枝对象时需要使用 XML::Twig::XPath(而不是 XML::Twig)。以下对我有用:

my $twig = XML::Twig::XPath->new();
$twig->parse( .... );
$twig->findnodes('//info_string[text()]');

构建具有完整 XPath 支持的 XML 树时,必须使用 XML::Twig::XPath->new 构建它 并且 添加的元素必须使用 [=12= 创建] 使 findnodes 工作。

由于在我的代码中使用 XPath 是可选的,因此我决定动态加载 XML::TwigXML::Twig::XPath,具体取决于是否需要完整的 XPath 支持。 此外,我的代码必须使用 XML::Twig::XPath::Elt 而不是 XML::Twig::Elt 以获得完整的 XPath 支持。

所以我的代码使用了这些技巧:

my $xml_twig = $use_xpath ? 'XML::Twig::XPath' : 'XML::Twig';
my $xml_twig_mod = $xml_twig . '.pm';
$xml_twig_mod =~ s,::,/,g;
require $xml_twig_mod;
my $xml = $xml_twig->new(...);
my $elt = "${xml_twig}::Elt"->new(...);

另见