使用XML :: TWIG

时间:2019-03-23 16:35:13

标签: xml perl xml-twig

我有这个XML文件,我需要一次访问一个特定的节点。以下是我的XML示例以及示例代码。

我的代码运行正常,除了我遍历所有Message / Content标签而不是仅在当前Message标签下获取特定的Message / Content标签。例如,当我只想返回1个()时,当处理当前Message标记时,我将取回3个Message / Content标记(带有refid =“ 123991123”的那个)。希望这是有道理的。任何帮助,我们将不胜感激。

代码:

my $twig = XML::Twig->new(
twig_handlers => {
    Selection => sub {
        foreach my $message ($_->findnodes('./Contents/Message')) {

            if($message->att('custom')){
                $Message_custom = $message->att('custom');
                foreach my $Content ($_->findnodes('./Contents/Message/Content')) {
                    print $Selection_id.": ".$Message_refid.": ".$TotalContents++."\n";
                    if($Content->att('language') eq "en"){
                        if($Content->att('imagelibraryid')){
                            $Message_Content_language_en_imagelibraryid = $Content->att('imagelibraryid');
                        }else{
                            $Message_Content_language_en = substr($message->field('Content'), 0, 20);
                        }
                    }
                }
            }
        }
    },
}
);

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <Selection id="54008473">
    <Name>Master</Name>
    <Contents>
      <Message refid="125796458" suppress="true" status="Unchanged"/>
      <Message refid="123991123" suppress="true" status="Unchanged">
        <Content language="en" imagelibraryid="5492396"/>
      </Message>
      <Message refid="128054778" custom="true" status="New">
        <Content language="en"><![CDATA[<p>Some English content</p>]]></Content>
        <Content language="fr"><![CDATA[<p>Some French content</p>]]></Content>
      </Message>
    </Contents>
  </Selection>
  <Selection id="54008475" datavaluerefid="54008479">
    <Name>RMBC</Name>
    <Contents>
      <Message refid="125796458" sameasparent="true" parentrefid="54008473" status="Unchanged"/>
      <Message refid="123991123" sameasparent="true" parentrefid="54008473" status="Unchanged"/>
      <Message refid="128054778" custom="true" status="New">
        <Content language="en"><![CDATA[<p>ada</p>]]></Content>
      </Message>
    </Contents>
  </Selection>
</Root>

1 个答案:

答案 0 :(得分:1)

这是第一次尝试基于XML的结构来理解您的代码应该做的事情:

    Selection节点的
  • handler在Content节点下的language == 'en'节点下寻找属性为Message的子节点Content
    • 转换为XPath ./Contents/Message/Content[@language='en']
    • 如果它具有属性imagelibraryid,请存储该属性的值
    • 否则,存储第一个孩子的CDATA内容
    • refid设置为父Message节点的属性值
  • 将它们添加到Selection节点的内容列表中
  • 要显示收集到的内容,请在数组ref上使用Data::Dumper
#!/usr/bin/perl
use warnings;
use strict;

use XML::Twig;
use Data::Dumper;

my %selections;

my $twig = XML::Twig->new(
    twig_handlers => {
        Selection => sub {
            #$_->print();
            print "selection id: ", $_->att('id'), "\n";

            my @contents;
            foreach my $content ($_->findnodes("./Contents/Message/Content[\@language='en']")) {
                my $result = {
                    refid => $content->parent->att('refid'),
                };
                my $id     = $content->att('imagelibraryid');
                if (defined $id) {
                    $result->{library} = $id;
                } else {
                    $result->{cata}    = $content->first_child->cdata;
                }
                push(@contents, $result);
            }

            # store collected Content nodes under selection ID
            $selections{ $_->att('id') } = \@contents;
        },
    }
);

$twig->parse(\*DATA);

while (my($id, $contents) = each %selections) {
    my $dump = Dumper($contents);
    print "Selection '${id}' messages: $dump\n";
}

exit 0;

__DATA__
<?xml version="1.0" encoding="UTF-8"?>
... the rest of your XML left out ...

试运行:

$ perl dummy.pl
selection id: 54008473
selection id: 54008475
Selection '54008473' messages: $VAR1 = [
          {
            'refid' => '123991123',
            'library' => '5492396'
          },
          {
            'cata' => '<p>Some English content</p>',
            'refid' => '128054778'
          }
        ];

Selection '54008475' messages: $VAR1 = [
          {
            'cata' => '<p>ada</p>',
            'refid' => '128054778'
          }
        ];