Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: XPath with node names and attributes...

by thunders (Priest)
on Oct 16, 2009 at 16:56 UTC ( #801595=note: print w/replies, xml ) Need Help??

in reply to XPath with node names and attributes...

Perhaps It would help if you provide some example code and xml that demonstrates the problem you are having.
  • Comment on Re: XPath with node names and attributes...

Replies are listed 'Best First'.
Re^2: XPath with node names and attributes...
by biswanath_c (Beadle) on Oct 16, 2009 at 17:33 UTC

    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

      Didn't have a chance to test your example, but I think this is the problem:

      $rule_source = "head/node1@attr1";

      Perl thinks you want to insert the contents of an array called @attr1 into the string because you used double quotes. Since there is no such array your $rule_source ends up with just "head/node1". Either escape the '@' or use a single quoted string as in the examples below:

      $rule_source = "head/node1\@attr1"; # or $rule_source = 'head/node1@attr1';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://801595]
[stevieb]: it's a reminder to re-inforce it :P
[atcroft]: james28909: That particular questions was a bit of trick, actually (depending on the country you are in). More interesting is, if you are trying to subtract from an epoch time, for instance, you might have to consider when/if DST occurs for a location,
[atcroft]: because you may have to adjust the number of seconds you change from an epoch from 86400 (not to mention leap seconds)....
[atcroft]: james28909: Although if your program is using a database, you might be able to "pass the buck" to the database and ask it to do the date change for you....

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2017-04-29 04:40 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.