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 8435717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:59:20+00:00 2026-06-10T06:59:20+00:00

I have the following Perl script with is meant to indent a XML file

  • 0

I have the following Perl script with is meant to indent a XML file correctly:

@files = glob "*.xml";

undef $/;
for $file (@files) {
    $indent = 0;
    open FILE, $file or die "Couldn't open $file for reading: $!";
    $_ = readline *FILE;
    close FILE or die "Couldn't close $file: $!";

    # Remove whitespace between > and < if that is the only thing separating them
    s/(?<=>)\s+(?=<)//g;

    # Indent
    s{  # Capture a tag <$1$2$3>,
        # a potential closing slash $1
        # the contents $2
        # a potential closing slash $3
        <(/?)([^/>]+)(/?)> 

        # Optional white space
        \s*

        # Optional tag.
        # $4 contains either undef, "<" or "</"
        (?=(</?))?
    }
    {
        # Adjust the indentation level.
        # $3: A <foo/> tag. No alteration to indentation.
        # $1: A closing </foo> tag. Drop one indentation level
        # else: An opening <foo> tag. Increase one indentation level
        $indent +=
            $3 ?  0 :
            $1 ? -1 :
                  1;

        # Put the captured tag back into place
        "<$1$2$3>" .
        # Two closing tags in a row. Add a newline and indent the next line
    ($1 and defined($4) and ($4 eq "</") ? "\n" . ("  " x $indent) : 
$4                                   ? "\n" . ("  " x $indent) :
                                       ""
        )
    # /g repeat as necessary
    # /e Execute the block of perl code to create replacement text
    # /x Allow whitespace and comments in the regex
    }gex;

    open FILE, ">", $file or die "Couldn't open $file for writing: $!";
    print FILE or die "Couldn't write to $file: $!";
    close FILE or die "Couldn't close $file: $!";
}

First, it’s indenting my tabs, and I wanted two whitespaces. Also, it’s producing tags in the same indentation to be on the same line, instead of in the next line, but with the same indent:

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE kit SYSTEM "tc.dtd"><kit><contact/><description>

where it is supposed to be:

  <?xml version="1.0" encoding="iso-8859-1"?>
  <!DOCTYPE kit SYSTEM "tc.dtd">
  <kit>
      <contact/>
      <description>

…

I acknowledge there are Perl tools to indent XML, such as XML-Tidy but due to tc.dtd tag, I always get an error complaining about unsolvable dependencies on the tc.dtd file, while I just care about the indentation of the same (formatting), not the dependencies itself.
What’s wrong with my Perl regex?

  • 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-10T06:59:21+00:00Added an answer on June 10, 2026 at 6:59 am

    You can use the tool xmllint which doesn’t necessarily validate. Example:

    Input (badly formatted):

    <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE kit SYSTEM "tc.dtd"> <kit>
      <contact/>      <description>chunky bacon</description> </kit>
    

    Run xmllint --format file.xml and you get:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE kit SYSTEM "tc.dtd">
    <kit>
      <contact/>
      <description>chunky bacon</description>
    </kit>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to maintain the following Perl script: #!/usr/bin/perl -w die Usage: $0 <file1>
the following perl script (Showing down) print the following XML file but the XML
I wrote the following Perl script (below) in order to create simple XML file.
Trying to get a simple test perl script working. Have the following files/folder structure
Today, I have written a perl script, as the following code: my @files =
I have the following perl script: $myVariable = some value; //... some code ...
I have an interesting problem. I wrote the following perl script to recursively loop
I have written a Perl script for the following bioinformatics question, but unfortunately there
I have the following code to open a browser in Perl $IE = Win32::OLE->new(InternetExplorer.Application);
I have the following code #!/usr/bin/perl use Tie::File; tie my @last_id, 'Tie::File', 'last_id.txt' or

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.