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?
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']} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With