Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:17:27+00:00 2026-06-17T08:17:27+00:00

I have a nested XML tags and need to append PF for ExternalId in

  • 0

I have a nested XML tags and need to append PF for ExternalId in Product XML

Input XML:

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

Desired output result:

   <Products>
        <Product>
          <ExternalId>PF317851</ExternalId>
        </Product>
        <Product>
          <ExternalId>PF316232</ExternalId>
        </Product>
        <Product>
          <ExternalId>PF13472</ExternalId>
        </Product>
    </Products>

I have tried using XML Simple.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:17:29+00:00Added an answer on June 17, 2026 at 8:17 am

    One way using the module XML::Twig.

    Content of script.pl:

    #!/usr/bin/env perl
    
    use warnings;
    use strict;
    use XML::Twig;
    
    {
        my $twig = XML::Twig->new( 
    
            twig_handlers => {
                'Product/ExternalId' => sub {
                    $_->prefix( 'PF' );
                }   
            },  
    
            pretty_print => 'indented',
    
        )->parsefile( shift )->print;
    }
    

    Run it like:

    perl-5.14.2 script.pl xmlfile
    

    That yields:

    <Products>                                                                                                                                                                                                                                   
      <Product>                                                                                                                                                                                                                                  
        <ExternalId>PF317851</ExternalId>                                                                                                                                                                                                        
      </Product>                                                                                                                                                                                                                                 
      <Product>                                                                                                                                                                                                                                  
        <ExternalId>PF316232</ExternalId>                                                                                                                                                                                                        
      </Product>
      <Product>
        <ExternalId>PF13472</ExternalId>
      </Product>
    </Products>
    

    UPDATE: To print to a file I’ve added two modifications, one line to open the output file, and the print method with the filehandle of the output file to print as argument

    It results in:

    #!/usr/bin/env perl
    
    use warnings;
    use strict;
    use XML::Twig;
    
    die qq|Usage: perl $0 <input-xml> <output-xml>\n| unless @ARGV == 2;
    
    open my $ofh, '>', pop or die qq|ERROR: Cannot open output file\n|;
    
    {
        my $twig = XML::Twig->new( 
    
            twig_handlers => {
                'Product/ExternalId' => sub {
                    $_->prefix( 'PF' );
                }   
            },  
    
            pretty_print => 'indented',
    
        )->parsefile( shift )->print( $ofh );
    }
    

    And it is invoked like:

    perl-5.14.2 script.pl xmlfile outfile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have nested XML tags and need to have the value of ExternalId in
I have an xml that has nested tags like the following: <?xml version=1.0 encoding=utf-8
I need to transform large XML files that have a nested (hierarchical) structure of
So I have XML with perfectly nested tags, however I'm ending up with weird
I have an input XML file which I need to copy 1:1 to the
I have a heavily nested XML document that I need to load into my
Hi currently I have a nested XMl , having the following Structure : <?xml
This is the scenario: I have nested classes and need to serialize then in
I have a nested simple XML structure that I load with PHP's simpleXML. Some
I have the following sample nested XML <?xml version=1.0 encoding=UTF-8?> <text> <main> <div n=Section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.