Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

XPath to read attributes using XML::LibXML ...

by biswanath_c (Beadle)
on Oct 18, 2009 at 00:35 UTC ( #801821=perlquestion: print w/ replies, xml ) Need Help??
biswanath_c has asked for the wisdom of the Perl Monks concerning the following question:


Hi,

I am using XML::LibXML module to parse an XML file and read attributes and node values using XPaths. I am able to read the value of nodes all right; but I am having trouble reading the values of attributes - I am not able to get the right approach (XPath and API to read attributes). Please help.

The sample XML file that I am trying to read:
<root> <head> <node1 attr1="Node1 Attribute"> Node1 Value </node1> </head> </root>

The PERL script that I am using to read attributes' values :
use XML::LibXML; $inp = $ARGV[0]; # create object my $parser = XML::LibXML->new(); # read input XML file my $inp_tree = $parser->parse_file($inp); my $inp_root = $inp_tree->getDocumentElement; my $root_text = $inp_root->toString(); $rule_source = "node1//@attr1"; my @inp_search_nodes = $inp_root->findnodes($rule_source); my $inp_search_results = scalar @inp_search_nodes; if ( 0 == $inp_search_results ) { print "XPath $rule_source not found! \n"; } else { my $path_text = $inp_search_nodes[ 0 ]->findvalue("."); print "Xpath ->: $path_text\n"; my $val = $inp_search_nodes[ 0 ]->textContent(); + print "Value of XPath $rule_source : $val \n"; }


I am trying to read the value of the attrbbute "attr1" of the node "node1".


When I use XPath "/head/node1/@attr1", I get this result when I try toi run the PERL scri[t shown above:
XPath error : Invalid expression /head/node1/ ^


When I use this XPath - "head/node1/@attr1", I get the same error listed above

When I use this XPath - "head/node1@attr1", the code prints the value of the node node1 and NOT the value of the attribute.

When I use this XPath - "/head/node1@attr1", I get this error: "XPath /head/node1 not found"


So, which Xpath should I use to read the value of the attribute of a node?




Comment on XPath to read attributes using XML::LibXML ...
Select or Download Code
Re: XPath to read attributes using XML::LibXML ...
by Khen1950fx (Canon) on Oct 18, 2009 at 05:52 UTC
    So, which Xpath should I use to read the value of the attribute of a node?

    This worked for me:

    my $rule_source = '//@attr1';
Re: XPath to read attributes using XML::LibXML ...
by Corion (Pope) on Oct 18, 2009 at 07:42 UTC

    If you used warnings and/or strict, Perl would tell you what you are doing wrong: You are using double quotes, which interpolate.

    Please put these two lines at the top of your script and fix all the errors. Then the XPath problem will have gone away:

    use strict; use warnings;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://801821]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-11-21 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (113 votes), past polls