Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl-XML::LibXML-获取具有特定属性的元素_Xml_Perl_Xpath_Xml Parsing_Xml Libxml - Fatal编程技术网

Perl-XML::LibXML-获取具有特定属性的元素

Perl-XML::LibXML-获取具有特定属性的元素,xml,perl,xpath,xml-parsing,xml-libxml,Xml,Perl,Xpath,Xml Parsing,Xml Libxml,我有个问题,我希望有人能帮我 我有以下示例xml结构: <library> <book> <title>Perl Best Practices</title> <author>Damian Conway</author> <isbn>0596001738</isbn> <pages>542</pages>

我有个问题,我希望有人能帮我

我有以下示例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>
预期输出:

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

但我没有得到任何回报

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

我肯定我忽略了一些非常明显的事情,但我无法找出我做错了什么


非常感谢你的帮助,谢谢你几乎就到了。请注意,
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";
}

你就快到了。请注意,
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";
}

XML属性没有文本节点,因此您的
$query
应该是
“//book/image[\@width='$width']/author/text()”
XML属性没有文本节点,因此您的
$query
应该是
“//book/image[\@width='$width']]/author/text()”
[Building in choroba的答案]

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

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";
    }
}

[乔洛巴的答案中的建筑]

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

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";
    }
}

Re“需要toString来打印值而不是引用”,真的吗?不适合我。@ikegami:如果我不使用它,我将得到
结果:XML::LibXML::Text=SCALAR(0x90e0230)
。自您的版本以来,字符串化重载必须已添加到XML::LibXML。很高兴知道它并不总是在那里。@ikegami:是的,我有一种感觉,我已经不需要它了,可能是另一台机器。Changelog显示“2.0012 Fri 9 Nov 06:39:32 IST 2012-修复对具有重载字符串化魔法的标量的引用的支持。”@choroba@ikegami,当使用诸如
//book/image/\@height
之类的东西来获取所有高度属性时(使用foreach循环),当我使用$data->toString打印时,它也会打印标签名,比如height=“190”、height=“190”、height=“125”,而不仅仅是190、190、125。您如何能够单独打印标记值和标记名称?谢谢“需要toString来打印值而不是引用”,真的吗?不适合我。@ikegami:如果我不使用它,我将得到
结果:XML::LibXML::Text=SCALAR(0x90e0230)
。自您的版本以来,字符串化重载必须已添加到XML::LibXML。很高兴知道它并不总是在那里。@ikegami:是的,我有一种感觉,我已经不需要它了,可能是另一台机器。Changelog显示“2.0012 Fri 9 Nov 06:39:32 IST 2012-修复对具有重载字符串化魔法的标量的引用的支持。”@choroba@ikegami,当使用诸如
//book/image/\@height
之类的东西来获取所有高度属性时(使用foreach循环),当我使用$data->toString打印时,它也会打印标签名,比如height=“190”、height=“190”、height=“125”,而不仅仅是190、190、125。您如何能够单独打印标记值和标记名称?谢谢