3

次のような XML ファイルがあります。

     <wave waveID="1">
        <well wellID="1" wellName="A1">
          <oneDataSet>
            <rawData>0.1123975676</rawData>
          </oneDataSet>
        <well>

次のコードで wellName 属性を出力しようとしています。

my @n1 = $xc->findnodes('//ns:wave[@waveID="1"]');  
  # so @n1 is an array of nodes with the waveID 1
  # Above you are searching from the root of the tree, 
  # for element wave, with attribute waveID set to 1.
foreach $nod1 (@n1) {  
  # $nod1 is the name of the iterator, 
  # which iterates through the array @n1 of node values.
my @wellNames = $nod1->getElementsByTagName('well');  #element inside the tree.
  # print out the wellNames :
foreach $well_name (@wellNames) {
   print $well_name->textContent;
   print "\n";
        }  

しかし、wellName を出力する代わりに、rawData 値 (例: 0.1123975676) を出力しています。理由がわかりませんよね?何が起こっているのかを理解するのに役立つようにコードにコメントを付けようとしましたが、コメントが間違っている場合は修正してください。ありがとう。

4

2 に答える 2

3

Assuming you want the wellName attribute of all well children of the particular wave, express that in XPath rather than looping by hand:

foreach my $n ($xc->findnodes(q<//ns:wave[@waveID='1']/ns:well/@wellName>)) {
    print $n->textContent, "\n";
}
于 2010-01-17T21:04:09.453 に答える
1

$node->attributes()属性ノードのリストを返します。

別の方法は、XPath を使用して途中まで行って残りを手動で行うのではなく、XPath 式を使用して属性ノードを直接フェッチすることです。

于 2010-01-17T20:08:48.793 に答える