How can I use a Perl variable inside a XPath expression using the XML::LibXML module's findnodes() function? This is what I got:
my $variable = 1;
foreach my $node1 ($doc->findnodes('par/par1/par2[@id = $variable]'))
{
}
But it doesn’t seem to work. Thanks.
You cannot interpolate using single quotes, you must use double quotes, or another form of quoting that does interpolate
"par/par1/par2[\@id = $variable]"
However, I assume that @id
is not a variable, so you must escape its sigil, or it will be treated as a variable as well. If you are using use strict
-- which you always should -- this will lead to a compiler error.
The other forms of quoting that might be mentioned
qq()
, which is exactly like double quote