snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:
I want to retrieve value of <release-date> element where <title> having value 'Solaris' and not other title elements.<?xml version="1.0"?> <playlist> <movie id="tt0112384"> <title>Apollo 13</title> <director>Ron Howard</director> <release-date>1995-06-30</release-date> </movie> <movie id="tt0307479"> <title>Solaris</title> <director>Steven Soderbergh</director> <release-date>2002-11-27</release-date> </movie> <movie id="tt1731141"> <title>Ender's Game</title> <director>Gavin Hood</director> <release-date>2013-11-01</release-date> </movie> </playlist>
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 Please do help !use warnings; use strict; use XML::XPath; use Data::Dumper; my $xml = '../events.xml'; my $xp = XML::XPath->new(filename => $xml); my $nodeset = $xp->findnodes('//playlist'); my $records = []; foreach my $node ($nodeset->get_nodelist) { my $title = $xp->find('./title', $node); push @$records, { title => $title->string_value, }; } print Dumper($records);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: get specific value of element using xpath in perl
by haukex (Archbishop) on Jun 23, 2017 at 13:22 UTC | |
by snehit.ar (Beadle) on Jun 24, 2017 at 01:08 UTC | |
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 |