in reply to Re^8: Xpath value query
in thread Xpath value query

because you had single quotes in [@tc='37'] in the query I couldn't just use ' query '.

Replies are listed 'Best First'.
Re^10: Xpath value query
by SDwerner (Initiate) on Sep 19, 2015 at 14:11 UTC

    OK, so one more problem I don't understand.. and I think it's more my lack of understanding about HASHES. I was getting an error on the my $value = $nodes[0]->text; statement indicating Can't call method "text" on an undefined. So, I thought I could use if (@node[0]) { my $value = $nodes[0]->text; } else { $value = "EMPTY_STRING"; } but now everything comes back as if the variable is undefined. How do I determine if a hash is undefined?

      @node is a different variable than @nodes.

      Also, hashes are accessed through $hash->{ key } not through $hash->key. Maybe see perldsc or perlreftut.

      You should really have this near the top of your program to enable Perl to tell you when you mistype variable names:

      use strict;

      Variables declared with my inside a block { .. } are not accessible outside the block. You probably want something like

      sub get_xpath_value { my ($xpath) = @_; my @nodes = $root->findnodes($xpath); my ($name,$value); # declare outside block if (defined $nodes[0]) { $value = $nodes[0]->text; # still accessible inside block $name = $nodes[0]->getName; } else { $value = 'not defined'; # or whatever you want $name = 'not defined'; } return ($name,$value); }
      poj
Re^10: Xpath value query
by SDwerner (Initiate) on Sep 19, 2015 at 13:14 UTC

    That makes sense now..