XML::LibXML の XPath を使用して XML ファイル内の要素にアクセスできないのはなぜですか?
-
21-09-2019 - |
質問
XML ファイルがあります。その一部は次のようになります。
<wave waveID="1">
<well wellID="1" wellName="A1">
<oneDataSet>
<rawData>0.1123975676</rawData>
</oneDataSet>
</well>
... more wellID's and rawData continues here...
Perl の libXML でファイルを解析し、以下を使用して WellName と rawData を出力しようとしています。
use XML::LibXML;
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file('/Users/johncumbers/Temp/1_12-18-09-111823.orig.xml');
my $xc = XML::LibXML::XPathContext->new( $doc->documentElement() );
$xc->registerNs('ns', 'http://moleculardevices.com/microplateML');
my @n = $xc->findnodes('//ns:wave[@waveID="1"]'); #xc is xpathContent
# should find a tree from the node representing everything beneath the waveID 1
foreach $nod (@n) {
my @c = $nod->findnodes('//rawData'); #element inside the tree.
print @c;
}
現時点では何も出力されません。Xpath ステートメントに問題があると思います。それを修正するのを手伝ってもらえますか、または xpath ステートメントのトラブルシューティング方法を教えていただけますか?ありがとう。
解決
使用する代わりに findnodes
ループ内で使用します getElementsByTagName():
my @c = $nod->getElementsByTagName('rawData');
処理を使用して次のような便利な方法をいくつか紹介します。 @c
配列:
$c[0]->toString; # <rawData>0.1123975676</rawData>
$c[0]->nodeName; # rawData
$c[0]->textContent; # 0.1123975676
他のヒント
「波」の要素が名前空間にある場合は、
は、その後、「生データ」要素は同様にあなたは、おそらく使用する必要があるようです。
foreach $nod (@n) {
my @c = $xc->findnodes('descendant::ns:rawData', $nod); #element inside the tree.
print @c;
}
所属していません StackOverflow