Reputation: 3626
I am having following XML which I required to process
<table>
<col1>check1</col1>
<col2>check2</col2>
<col3>check3</col3>
<content>
<data>gt1</data>
<data>check_gt1</data>
</content>
<content>
<data>gt2</data>
<data>check_gt2</data>
</content>
</table>
I wrote the following code to process this XML
my $parser = XML::LibXML->new(); my $respDom = $parser->parse_string($xmldata); my @rowNodes = $respDom->getDocumentElement->findnodes("//content"); if(scalar @rowNodes > 0) { print "\nRow nodes size is ".scalar @rowNodes."\n"; foreach my $rowNode (@rowNodes) { my $colNode = $rowNode->findnodes("//data")->[0]; my $num = $colNode->textContent; print "\nNUM is ".$colNode; } }
My result showing the size of node "content" is 2. But it returns "gt1" two times inside the loop, It is not returning "gt2" in the second iteration.
Is there anything I need to change on this ?
Upvotes: 1
Views: 155
Reputation: 54323
Your XPath expression for finding the data
node is wrong. An XPath with //foo
will search any element with that name. It searches in the whole document tree, not only below that node. And with your ->[0]
it will always return the first match, which is gt1
for the full document.
Remove the slashes.
my $colNode = $rowNode->findnodes("data")->[0];
To check that $rowNode
actually contains the full document tree, but only points to the current node, try this:
my $colNode = $rowNode->findnodes("../content/data")->[0];
It will also give you gt1
twice.
Upvotes: 1