【问题标题】:Does Mojo::DOM provide the possibility to use nested syntax?Mojo::DOM 是否提供使用嵌套语法的可能性?
【发布时间】:2011-12-21 10:42:55
【问题描述】:

如何使用 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;
}

【问题讨论】:

    标签: xml perl mojolicious


    【解决方案1】:

    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;
    

    【讨论】:

      【解决方案2】:

      嗯,从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
      

      【讨论】:

        猜你喜欢
        • 2012-09-30
        • 2021-08-29
        • 1970-01-01
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 2014-05-16
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多