in reply to Re^6: Xpath value query
in thread Xpath value query
This was one problem, you don't need to my $xpath = "q!" . $argvalx . "!";
see http://perldoc.perl.org/perlop.html#Quote-Like-Operators
and the second XPATH was missing a [] here [@tc="8"]].
Assuming you will only get a single match try
Update : added getName and simplified#!perl use strict; use XML::Twig::XPath; my $xmlfile = $ARGV[0] || 'txlife.xml'; my $twig = XML::Twig::XPath->new(); $twig->parsefile($xmlfile); my $root = $twig->root; my @XPV = (); $XPV[0] = '//Party[@id=//Relation[child::RelationRoleCode[@tc="37"]]/@ +RelatedObjectID]/Producer/CarrierAppointment/CompanyProducerID'; $XPV[1] = '//OLifE/Party[@id=//OLifE/Relation[RelationRoleCode[@tc=8]] +/@RelatedObjectID]/Person/FirstName'; for my $i (0..1){ my ($name,$value) = get_xpath_value($XPV[$i]); print " $name : $value\n"; } sub get_xpath_value { my ($xpath) = @_; my @nodes = $root->findnodes($xpath); my $value = $nodes[0]->text; my $name = $nodes[0]->getName; return ($name,$value); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Xpath value query
by SDwerner (Initiate) on Sep 18, 2015 at 18:44 UTC | |
by poj (Abbot) on Sep 18, 2015 at 18:58 UTC | |
by SDwerner (Initiate) on Sep 19, 2015 at 14:11 UTC | |
by Corion (Patriarch) on Sep 19, 2015 at 14:50 UTC | |
by poj (Abbot) on Sep 19, 2015 at 15:07 UTC | |
by SDwerner (Initiate) on Sep 19, 2015 at 13:14 UTC |