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

  • Home
  • SEARCH
  • 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 9160359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:43:14+00:00 2026-06-17T13:43:14+00:00

I am new to XML::Twig . How can I change all empty elements to

  • 0

I am new to XML::Twig. How can I change all empty elements to use empty-element tags (<foo/>) instead of a start-tag and end-tag combo (<foo></foo>)?

Input:

<book>
    <given-names>Maurice<xref ref-type="fn" rid="fnI_1"></xref></given-names>
    <colspec colname="col1" colnum="1"></colspec>
    <entry align="left"><p></p></entry>
</book>

I need output as:

<book>
    <given-names>Maurice<xref ref-type="fn" rid="fnI_1"/></given-names>
    <colspec colname="col1" colnum="1"/>
    <entry align="left"><p/></entry>
</book>

I tried:

       use XML::Twig;
       my $xml = XML::Twig->new(twig_handlers => {
                                  'xref' => sub {$_->set_tag('#EMPTY'),},
                                },
                                pretty_print => 'indented',                                        
                               );
       $xml->parse('sample.xml');
       $xml->print;
}

But I can’t process it. How can change gloabally without content tag to empty tag? how can I change?

  • 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-17T13:43:15+00:00Added an answer on June 17, 2026 at 1:43 pm

    If you want to stick with Twig, you can do it like this:

    #!usr/bin/perl
    use strict;
    use warnings;
    use XML::Twig;
    
    my $xml = XML::Twig->new(twig_handlers => {
                 'p' => sub { 
                     if (!$_->first_child()) { $_->set_content('#EMPTY') } 
                  },
               },
               pretty_print => 'indented',
               empty_tags => 'normal'                                 
    );
    
    $xml->parsefile('file.xml');
    $xml->print;
    

    Basically you have to manually check if the element contains nothing, then set it to be an empty element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I new to xml twig, how can I add space between two elements in
I am new XML-Twig... I want split para tag.... XML File: <xml> <p class=indent>text
I want collect all tags in from XML file. How can I remove comments
#!/usr/bin/perl use strict; use warnings FATAL => 'all'; use 5.010; use XML::Twig; sub get_xml_from_data
How can I return the entire xml tag using xml::twig and save it to
I'm new to querying XML datatype in SQL Server 2005. Anyone can help me
I can't seem to figure out how to create a new XML file in
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use XML::Twig; use HTTP::Request; my @joblist =
how we can create new xml file by C# in the following situation: its
I am still new with using XML::Twig . From the main program I look

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.