Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

okay! here we go. I am just giving an example here that I tested and which demonstrates my problem:


The XML:
<root> <head> <node1 attr1="Node1 Attribute"> Node1 Value </node1> </head> </root>


The PERL script that tries to read the attribute:

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 = "head/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"; }

If i search for XPath head/node1@attr1 , the code works fine and fetches the value of the node.

If i use XPath XPath /head/node1@attr1 , the program gives the error :
 "/head/node1 not found!"

If i use the Xpath, i get this error:
 Invalid expression

If I use the XPath "/head/node1/@attr1", I get this error:
 Invalid expression



In reply to Re^2: XPath with node names and attributes... by biswanath_c
in thread XPath with node names and attributes... by biswanath_c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-19 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found