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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:00:38+00:00 2026-05-15T15:00:38+00:00

Sometimes XML files needs to be stored in some VCS. Such files are often

  • 0

Sometimes XML files needs to be stored in some VCS. Such files are often edited using GUI tools which can reorder the elements each times as they want.

Also VCS merging is usually line-oriented, and often XML files either looks likes one long line or fully indented like

<foo>
    <bar>
        <name>
            n3
        </name>
        <value>
            qqq3
        </value>
    </bar>
    <bar>
        <name>
            n2
        </name>
        <value>
            qqq2
        </value>
    </bar>
</foo>

, while they should look like

<foo>
    <bar>  <name> n2 </name>  <value> qqq2 </value> </bar>
    <bar>  <name> n3 </name>  <value> qqq3 </value> </bar>
</foo>

(e.g. “partially indented”) to be more human readable/editable, compact. One simple logical unit should occupy one line.

Even if somebody converts XML file to such nice format, someone else will edit it in GUI tool that will reorder and reintent everything and it will be bad (unreadable and VCS will report massive changes despite of there are almost no actual changes).

Is there ready made XSLT transformation (or other program) that converts all XML files to some unified format (e.g. sorts (if element order do not matter) and unifies whitespace) and where I can specify which elements should be oneliners?

For example, if I can specify such transformation as filter in .gitattributes and git will automatically handle this.

  • 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-05-15T15:00:38+00:00Added an answer on May 15, 2026 at 3:00 pm

    Created my own sort-indenter based on http://www.dpawson.co.uk/xsl/sect2/pretty.html and Alejandro’s answer: http://vi-server.org/vi/sortindent.xsl. Mirrored here:

    <!-- Change 'oneliner' to the name of element you want to see as one line -->
    <!-- Remove 'xsl:sort' element if you don't want sorting -->
    <!-- http://stackoverflow.com/questions/3157658/converting-xml-files-to-be-human-editable-and-managable-by-vcs/3160818#3160818 -->
    
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml"/>
       <xsl:param name="indent-increment" select="'   '" />
    
       <xsl:template match="*">
          <xsl:param name="skip_indent" select="name()='oneliner' or name()='another_oneliner'"/> 
          <xsl:param name="indent" select="'&#xA;'"/>
    
          <xsl:if test="not($skip_indent)">
          <xsl:value-of select="$indent"/>
    
          <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates>
            <xsl:with-param name="indent" select="concat($indent, $indent-increment)"/>
            <xsl:sort select="@*|node()"/> 
            </xsl:apply-templates>
            <xsl:if test="*">
              <xsl:value-of select="$indent"/>
            </xsl:if>
    
          </xsl:copy>
    
          </xsl:if>
          <xsl:if test="$skip_indent">
             <xsl:value-of select="$indent"/>
             <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates>
               <xsl:with-param name="indent" select="' '"/>
               <xsl:with-param name="skip_indent" select="1"/>
               <xsl:sort select="@*|node()"/> 
                </xsl:apply-templates>
             </xsl:copy>
          </xsl:if>
       </xsl:template>
    
       <xsl:template match="comment()|processing-instruction()">
          <xsl:copy />
       </xsl:template>
    
       <xsl:template match="text()">
           <xsl:param name="skip_indent" select="0"/> 
           <xsl:if test="$skip_indent">
               <xsl:value-of select="normalize-space(.)"/>
           </xsl:if>
           <xsl:if test="not($skip_indent)">
               <xsl:if test="not(normalize-space(.)='')">
                  <xsl:value-of select="."/>
               </xsl:if>
           </xsl:if>
       </xsl:template>
    
    
    </xsl:stylesheet>
    

    Now “equivalent” transformations of the original XML file (except of reordering of attributes) maps to the same resulting XML, and it is fine formatted, and I can force some elements to be oneliners.

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

Sidebar

Related Questions

Sometimes I need to quickly extract some arbitrary data from XML files to put
For my application, I need to serialize data to file using XML, so I
Where I am: Linux command line The problem I have now: Sometimes I couldn't
I currently store a serialized XML file in the application directory that contains all
I've got a ant build.xml that uses the <copy> task to copy a variety
I recently started using Eclipse at work for my Java servlet projects. I've been
I have a ASP.NET MVC application that runs in both IIS 6 and 7.
i need to run a really long php script (four and half, five hours).
I have a body of text that I have to scan and each line

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.