You were almost there. Just notice that author
is not a child of image
. Attributes do not have text() children, you can compare their values directly with strings. Also, toString
is needed to print the values out instead of references.
#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML;
my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file('1.xml');
my $width = "145";
my $query = "//book[image/\@width = '$width']/author/text()";
foreach my $data ($xmldoc->findnodes($query)) {
print "Results: ", $data->toString, "\n";
}