我编写了一些Perl模块来构建XML::Twig树。不幸的是,XML::Twig
的XPath实现似乎不完整,所以我加载了XML::Twig::XPath
。
由于XML::Twig的文档声明findnodes
与get_xpath
(在其他几个中)相同,我期望我可以处理像//info_string[text()]
这样的查询,而原始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()
方法返回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
当您构建twig对象时,似乎需要使用XML::Twig::XPath
(而不是XML::Twig
)。以下内容适合我:
my $twig = XML::Twig::XPath->new();
$twig->parse( .... );
$twig->findnodes('//info_string[text()]');
在构建具有完整XPath支持的XML树时,必须使用XML::Twig::XPath->new
构建,并且必须使用XML::Twig::XPath::Elt->new
创建添加的元素以使findnodes
工作。
由于在我的代码中使用XPath是可选的,所以我决定动态加载XML::Twig
或XML::Twig::XPath
,这取决于是否需要完全的XPath支持。此外,为了完全支持XPath,我的代码必须使用XML::Twig::XPath::Elt
而不是XML::Twig::Elt
。
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(...);
参见https://stackoverflow.com/a/5790563/6607497