2011-12-21 10 views
3

Mojo :: DOMモジュールでこの例を書くにはどうすればよいですか?Mojo :: DOMはネストされた構文を使用できますか?

#!/usr/bin/env perl 
use warnings; 
use 5.012; 
use XML::LibXML; 

my $string =<<EOS; 
<result> 
    <cd> 
    <artists> 
     <artist class="1">Pumkinsingers</artist> 
     <artist class="2">Max and Moritz</artist> 
    </artists> 
    <title>Hello, Hello</title> 
    </cd> 
    <cd> 
    <artists> 
     <artist class="3">Green Trees</artist> 
     <artist class="4">The Leons</artist> 
    </artists> 
    <title>The Shield</title> 
    </cd> 
</result> 
EOS 
#/ 
my $parser = XML::LibXML->new(); 
my $doc = $parser->load_xml(string => $string); 
my $root = $doc->documentElement; 

my $xpath = '/result/cd[artists/artist[@class="2"]]/title'; 

my @nodes = $root->findnodes($xpath); 
for my $node (@nodes) { 
    say $node->textContent; 
} 

答えて

5

Mojo :: DOMはCSS3セレクタをサポートしていますが、これは驚くべきことですがxpathほど多用途ではありません。 CSS3は、ノードに降りた後に上昇する方法を提供しません。それはだけれども

あなたは、同じことを達成することができ、より複雑ビット:

Mojo::DOM->new($string) 
    ->find('result:root > cd > artists > artist.2') 
    ->each(sub { say shift->parent->parent->title->text }); 

または

say $_->parent->parent->title->text 
    for Mojo::DOM->new($string) 
    ->find('result:root > cd > artists > artist.2')->each; 
3

お、それは明らかですhttp://search.cpan.org/perldoc/Mojo::DOM#SYNOPSIS

use Mojo::DOM; 
my $string =<<EOS; 
<result> 
    <cd> 
    <artists> 
     <artist class="1">Pumkinsingers</artist> 
     <artist class="2">Max and Moritz</artist> 
    </artists> 
    <title>Hello, Hello</title> 
    </cd> 
    <cd> 
    <artists> 
     <artist class="3">Green Trees</artist> 
     <artist class="4">The Leons</artist> 
    </artists> 
    <title>The Shield</title> 
    </cd> 
</result> 
EOS 
my $dom = Mojo::DOM->new ($string); 
my $xpath = '/result/cd[artists/artist[@class="2"]]/title'; 
print "\n1 ", $dom->find($xpath); 
print "\n2 ", $dom->find('.2'); 
print "\n3 ", $dom->find('artist.2'); 
print "\n4 ", $dom->find('artists artist.2'); 
print "\n5 ", $dom->find('artists artist.2')->[0]->parent; 
print "\n6 ", $dom->find('artists artist.2')->[0]->parent->parent; 
print "\n7 ", $dom->find('artists artist.2')->[0]->parent->parent->find('title'); 
print "\n8 ", $dom->find('artists artist.2')->[0]->parent->parent->find('title')->[0]->text; 
print "\n9 ", $dom->find('artists artist.2')->[0]->parent->parent->find('title')->first->text; 
$dom->find('artists artist.2')->each(sub { 
    print "\n10 ", $_[0]->parent->parent->find('title')->first->text; 
}); 
__END__ 

1 
2 <artist class="2">Max and Moritz</artist> 
3 <artist class="2">Max and Moritz</artist> 
4 <artist class="2">Max and Moritz</artist> 
5 <artists> 
     <artist class="1">Pumkinsingers</artist> 
     <artist class="2">Max and Moritz</artist> 
    </artists> 
6 <cd> 
    <artists> 
     <artist class="1">Pumkinsingers</artist> 
     <artist class="2">Max and Moritz</artist> 
    </artists> 
    <title>Hello, Hello</title> 
    </cd> 
7 <title>Hello, Hello</title> 
8 Hello, Hello 
9 Hello, Hello 
10 Hello, Hello 
関連する問題