I have an example xml
<?xml version="1.0" ?>
<Details date="2022-02-09" ver="1">
<VerNum>/14</VerNum>
<Info>
<model>S22</model>
<branch name="city_1">
<prevstock>10000</prevstock>
<def>1</def>
</branch>
<branch name="city_2">
<presstock>2000</presstock>
<def>2</def>
</branch>
<branch name="city_3">
<futstock>3000</futstock>
<def>0.3</def>
</branch>
</Info>
</Details>
I need to access the stock, tag name is not always consistent and I can't depend on position of node too, I do not understand the correct usage of XPath / ends-with function.
use warnings;
use strict;
use feature 'say';
use Data::Dumper;
use XML::LibXML;
my $file = "ex.xml";#// die "Usage: $0 filename\n";
my $parser = XML::LibXML->load_xml(location => $file);
my %branch_stock;
foreach my $sec ($parser->findnodes('/Details/Info')) {
for my $branch ($sec->findnodes('./branch')) {
my $branch_name = $branch->getAttribute('name');
my $stock_value = $branch->findnodes('*[ends-with(name(),"stock")]')->[0]->textContent;
#say "$branch_name --> $stock_value";
$branch_stock{$branch_name} = $stock_value;
}
}
say Dumper \%branch_stock;
This gives me error,
error : xmlXPathCompOpEval: function ends-with not found
XPath error : Unregistered function
XPath error : Stack usage errror
error : xmlXPathCompiledEval: 2 objects left on the stack.
Could anyone please help understand the problem and help overcome please ? Thanks a lot in advance.