F
Faith Greenwood
How do I return multiple nodes in an xpath search using LibXML? I have
the following xml:
<entry>
<set>
<nodeA>A</nodeA>
<book><name>Near Dead</name><date>1992</date></book>
<page>1262</page>
</set>
<set>
<nodeA>A</nodeA>
<book><name>Alive and Well</name><date>1973</date></book>
<page></page>
</set>
<set>
<nodeA>A</nodeA>
<book><name>Still Kicking</name><date>1968</date></book>
<page>1598</page>
</set>
</entry>
Here is my perl:
my $parser=XML::LibXML->new();
my $doc=$parser->parse_file("xml.xml");
my @array;
my $search="//entry/set[nodeA/text()='A']/page/text()";
push(@array,$_->data) for ($doc->findnodes($search));
print "@array\n";
#######
When I print out @array, I get "1262 1598", as expected. The problem
is I don't know the book that 1598 belongs to. Does 1598 belong to
"Still Kicking" or "Alive and Well"? Further, I need to keep track of
the page AND the date AND the author, without mixing any of them up.
If I do 2 diff. searches for the page and dates and push those into 2
separate arrays, obviously I won't be able to use them in the correct
order.
Does anyone have any suggestions?
thx!
the following xml:
<entry>
<set>
<nodeA>A</nodeA>
<book><name>Near Dead</name><date>1992</date></book>
<page>1262</page>
</set>
<set>
<nodeA>A</nodeA>
<book><name>Alive and Well</name><date>1973</date></book>
<page></page>
</set>
<set>
<nodeA>A</nodeA>
<book><name>Still Kicking</name><date>1968</date></book>
<page>1598</page>
</set>
</entry>
Here is my perl:
my $parser=XML::LibXML->new();
my $doc=$parser->parse_file("xml.xml");
my @array;
my $search="//entry/set[nodeA/text()='A']/page/text()";
push(@array,$_->data) for ($doc->findnodes($search));
print "@array\n";
#######
When I print out @array, I get "1262 1598", as expected. The problem
is I don't know the book that 1598 belongs to. Does 1598 belong to
"Still Kicking" or "Alive and Well"? Further, I need to keep track of
the page AND the date AND the author, without mixing any of them up.
If I do 2 diff. searches for the page and dates and push those into 2
separate arrays, obviously I won't be able to use them in the correct
order.
Does anyone have any suggestions?
thx!