Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "Invalid predicate" error from getElementsByTagName?

Tags:

xml

perl

I have the following XML:

<config>
  <version general="1.2.3">
    <subtype type="a" version="1.2" />
    <subtype type="b" version="3.6" />
    ...
  </version>
  ...
</config>

I have some code in Perl to get the config node from a database.

After I get it, if I try the following:

my $elem = $cfg->getElementsByTagName("version");
my $generalVer = $elem ? $elem->get_node(1)->getAttribute("general") : undef;

all works fine, $generalVer contains 1.2.3, as expected.

But if I try this:

my $elem = $cfg->getElementsByTagName("version/subtype[@type='a']");
my $aVersion = $elem ? $elem->get_node(1)->getAttribute("version") : undef;

It fails with the message "Invalid predicate".

Can someone help with this issue?

like image 270
Dikla Avatar asked Dec 02 '22 06:12

Dikla


1 Answers

I strongly suspect that "version/subtype[@type='a']" is not, in fact, a tag name. That looks like an XPath query.

I'm assuming you're using something like XML::DOM to parse this XML. If you want to use XPath, then you can use XML::DOM::XPath, which adds XPath support.

For example,

 use XML::DOM;
 use XML::DOM::XPath;
 ...
 my $elem = $cfg->findnodes( q{//version/subtype[@type='a']} );
like image 128
friedo Avatar answered Dec 04 '22 22:12

friedo