1

ExternalId我已经嵌套了XML 标记,并且需要将ProductXML 中的值添加到ProductPageURL使用XML::Twig.

<Products>
    <Product>
      <ExternalId>317851</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>316232</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>13472</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
</Products>

预期结果:

<Products>
    <Product>
      <ExternalId>PF317851</ExternalId>
      <ProductPageUrl>317851</ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>PF316232</ExternalId>
      <ProductPageUrl>316232</ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>PF13472</ExternalId>
      <ProductPageUrl>13472</ProductPageUrl>
    </Product>
    </Products>

我使用以下逻辑XML::Twig

my $twig = XML::Twig->new( 
        twig_handlers => {      
            'Product/ExternalId' => sub {
                $_->prefix( 'PF' );
            },
             'Product/ProductPageUrl' => sub {
                $_->set_text($_->get('Product/ExternalId'));
            }, 
                
        },
        pretty_print => 'indented',
    keep_encoding => 1,
    )->parsefile($xml_path_filename )->print_to_file($xml_path_filename);

你能告诉我如何使代码更容易吗?我无法达到预期的结果。

4

2 回答 2

2

这是一种方法:

use warnings;
use strict;
use XML::Twig;

my $twig = XML::Twig->new( 
    twig_handlers => {      
        Product => \&prod,
    },
    pretty_print => 'indented',
    keep_encoding => 1,
);

$twig->parse(q(
<Products>
    <Product>
      <ExternalId>317851</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>316232</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>13472</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
</Products>
));

$twig->print();

sub prod {
    my ($t, $prod) = @_;
    my $extid = $prod->first_child('ExternalId');
    my $id = $extid->text();
    $extid->prefix('PF');
    my $url = $prod->first_child('ProductPageUrl');
    $url->set_text($id);
}

输出

<Products>
  <Product>
    <ExternalId>PF317851</ExternalId>
    <ProductPageUrl>317851</ProductPageUrl>
  </Product>
  <Product>
    <ExternalId>PF316232</ExternalId>
    <ProductPageUrl>316232</ProductPageUrl>
  </Product>
  <Product>
    <ExternalId>PF13472</ExternalId>
    <ProductPageUrl>13472</ProductPageUrl>
  </Product>
</Products>
于 2013-01-17T17:00:07.870 回答
2

您的初始代码中有两个问题:首先我不认为get是 XML::Twig::Elt 方法。然后您首先为ExternalId文本添加前缀,然后(一旦添加前缀)尝试使用它来更新ProductPageUrl. 那是行不通的。在这种情况下,我认为最好为Product标签使用一个处理程序,在其中获取 id 数据,然后更新两个子元素。

这是一个解决方案,作为测试编写,因此如果您的输出发生变化,更容易更新:

#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 1;

use XML::Twig;

# in and expected are in the DATA section, separated by 2 \n
my( $in, $expected)= do { local $/="\n\n"; <DATA>};

my $t= XML::Twig->new( twig_handlers => { Product => \&update_product },
                       keep_spaces => 1,
                     )     
                 ->parse( $in);

is( $t->sprint, $expected, "one test to rule them all");

sub update_product
  { my( $t, $product)= @_;
    my $id= $product->field( 'ExternalId');
    $product->first_child( 'ExternalId')->prefix( 'PF');
    $product->first_child( 'ProductPageUrl')->set_text( $id);
  }

__DATA__
<Products>
    <Product>
      <ExternalId>317851</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>316232</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>13472</ExternalId>
      <ProductPageUrl></ProductPageUrl>
    </Product>
</Products>

<Products>
    <Product>
      <ExternalId>PF317851</ExternalId>
      <ProductPageUrl>317851</ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>PF316232</ExternalId>
      <ProductPageUrl>316232</ProductPageUrl>
    </Product>
    <Product>
      <ExternalId>PF13472</ExternalId>
      <ProductPageUrl>13472</ProductPageUrl>
    </Product>
</Products>
于 2013-01-17T17:14:30.930 回答