1

我有一个问题希望有人能帮忙解决...

我有以下示例 xml 结构:

<library>
    <book>
       <title>Perl Best Practices</title>
       <author>Damian Conway</author>
       <isbn>0596001738</isbn>
       <pages>542</pages>
       <image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif"
            width="145" height="190" />
    </book>
    <book>
       <title>Perl Cookbook, Second Edition</title>
       <author>Tom Christiansen</author>
       <author>Nathan Torkington</author>
       <isbn>0596003137</isbn>
       <pages>964</pages>
       <image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif"
            width="145" height="190" />
    </book>
    <book>
       <title>Guitar for Dummies</title>
       <author>Mark Phillips</author>
       <author>John Chappell</author>
       <isbn>076455106X</isbn>
       <pages>392</pages>
       <image src="http://media.wiley.com/product_data/coverImage/6X/0750/0766X.jpg"
           width="100" height="125" />
    </book>
</library>

我认为应该工作的代码:

use warnings;
use strict;

use XML::LibXML;

my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file('/path/to/xmlfile.xml');

my $width = "145";

my $query = "//book/image[\@width/text() = '$width']/author/text()";

foreach my $data ($xmldoc->findnodes($query)) {
    print "Results: $data\n";
}

预期输出:

达米安·康威汤姆·克里斯蒂安森

但我没有得到任何回报。

我认为这将匹配“book”元素中任何“author”元素的文本内容,该元素还包含一个“image”元素,其属性“width”的值为 145。

我确定我在这里忽略了一些非常明显的东西,但无法弄清楚我做错了什么。

非常感谢您的帮助谢谢

4

3 回答 3

4

你快到了。请注意,这author不是image. 属性没有 text() 子项,您可以直接将它们的值与字符串进行比较。此外,toString需要打印值而不是引用。

#!/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";
}
于 2013-08-15T09:36:41.443 回答
1

[建立在choroba的答案中]

在插值不安全的情况下$width(例如,如果它可能包含 a '),您可以使用:

for my $book ($xmldoc->findnodes('/library/book')) {
    my $image_width = $book->findvalue('image/@width');
    next if !$image_width || $image_width ne '145';

    for my $data ($book->findnodes('author/text()')) {
        print "Results: ", $data->toString, "\n";
    }
}
于 2013-08-15T14:53:49.567 回答
0

XML 属性没有文本节点,所以你$query应该是"//book/image[\@width='$width']/author/text()"

于 2013-08-15T09:38:19.903 回答