in reply to get specific value of element using xpath in perl
I want to retrieve value of <release-date> element where <title> having value 'Solaris' and not other title elements. ... I tired XPath something like my $title= $xp->find("/playlist/movie[title='Solaris']/release-date", $node); But result is not as expected OutPut ::2002-11-27
If the question is what is the <release-date> of the movie with the <title> "Solaris", isn't the correct answer "2002-11-27"? Both XML::LibXML and XML::XPath seem to work fine for me with the exact XPath expression you've shown. <update> All that the code you showed seems to try to do is build an array of hashes containing the <title> elements' values, which doesn't seem to fit your problem statement. </update>
my $file = '/tmp/1193350.xml'; my $xpath = q{/playlist/movie[title='Solaris']/release-date}; use XML::LibXML; my $doc = XML::LibXML->load_xml(location=>$file); for my $node ( $doc->findnodes($xpath) ) { print "XML::LibXML: ",$node->textContent,"\n"; } use XML::XPath; my $xp = XML::XPath->new(filename=>$file); my $nodeset = $xp->find($xpath); for my $node ($nodeset->get_nodelist) { print " XML::XPath: ",$node->string_value,"\n"; } __END__ XML::LibXML: 2002-11-27 XML::XPath: 2002-11-27
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: get specific value of element using xpath in perl
by snehit.ar (Beadle) on Jun 24, 2017 at 01:08 UTC | |
|
Re^2: get specific value of element using xpath in perl
by snehit.ar (Beadle) on Jun 27, 2017 at 08:22 UTC | |
by haukex (Archbishop) on Jun 27, 2017 at 08:44 UTC | |
by snehit.ar (Beadle) on Jun 27, 2017 at 09:07 UTC |