XML-LibXML - using context node to xpath expression

Posted on Wed Jul 11 22:51:01 2007 by jimw
using context node to xpath expression
i have the following demo xml from Learning XML:
<?xml version="1.0"?> <quotelist> <quotation style="wise" id="q1"> <text>Expect nothing; be ready for anything.</text> <source>Samuri Chant</source> </quotation> <quotation style="political" id="q2"> <text>If one morning I walked on top of the water across the Potomac River, the headline that afternoon would read "President Can't Swim."</text> <source>Lyndon B. Johnson</source> </quotation> <quotation style="silly" id="q3"> <?human laugh?> <text>What if the hokey-pokey IS what it's all about?</text> </quotation> <quotation style="wise" id="q4"> <text>If they give you ruled paper, write the other way.</text> <source>Juan Ramon Jiminez</source> </quotation> <!-- the checkbook is mighter than the sword? --> <quotation style="political" id="q5"> <text>Banking establishments are more dangerous than standing armies.</text> <source>Thomas Jefferson</source> </quotation> </quotelist>
should both of the following produce "q5"?:
use warnings; use strict; use XML::LibXML; + my $parser = XML::LibXML->new; my $doc = $parser->parse_file( 'practice.xml' ); print "\nxpath3\n"; # starting with "quotelist", get the last "quotation", go down to it's "source", # and get that node. with that node subsequent xpath queries can be made such # as get the attribute value of the element above. my $xpath3 = '/quotelist/quotation[last()]/source'; my @nodes3 = $doc->findnodes( $xpath3 ); my $result3 = $nodes3[0]->find( '../@id' ); print $result3->string_value, "\n"; + print "\nxpath4 -- does not work yet\n"; # does not work yet. should not i be able to optionally specify context node my $xpath4 = '/quotelist/quotation[last()]/source'; my @nodes4 = $doc->findnodes( $xpath4 ); my $result4 = $doc->find( '../@id', $nodes4[0] ); print $result4->string_value, "\n"; + exit 0;
Write a response