I have a nested XML tags and need to have the value of ExternalId in Product XML to a ProductPageURL tag using 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>
Expected result:
<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>
I have using the below logic using 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);
Could you let me know how to make the code easier, I am not able to acheive the expected result for now
There are 2 problems in your initial code: first I don't think
get
is an XML::Twig::Elt method. Then you first prefix theExternalId
text, then (once prefixed) try to use it to updateProductPageUrl
. That won't work. In this case I think you're better off having a single handler, for theProduct
tag, in which you get the id data, then update both sub-elements.Here a solution, written as a test so it's easier to update if your output changes:
Here is one way:
output