ExternalId
我已经嵌套了XML 标记,并且需要将Product
XML 中的值添加到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);
你能告诉我如何使代码更容易吗?我无法达到预期的结果。