我正在使用 XML::Twig 来处理这个 XML:
<?xml version="1.0" encoding="UTF-8"?>
<termEntry>
<langSet lang="en">
<ntig>
<termGrp>
<term>trail</term>
<termNote type="partOfSpeech">noun</termNote>
</termGrp>
<descrip type="context">Like in a forest</descrip>
</ntig>
</langSet>
</termEntry>
我正在使用以下代码来处理它:
use strict;
use XML::Twig;
my $twig_handlers = {
termEntry => sub { for my $node($_[1]->findnodes('.//descrip|.//termNote')){print $node->text;}},
};
my $twig= new XML::Twig(
TwigRoots => { termEntry => 1},
TwigHandlers => $twig_handlers,
);
$twig->parsefile('C:\Users\me\file.xml');
代码失败:
error in xpath expression .//descrip|.//termNote around descrip|.//termNote at
C:\Users\nate\Desktop\test.pl line 6
我一直在尝试不同的东西,任何时候我都使用'|' xpath 中的字符会破坏程序。它在http://www.xpathtester.com上工作得很好(以为我用 '//' 替换了 '.')。有想法该怎么解决这个吗?