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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:40:59+00:00 2026-05-15T19:40:59+00:00

I have some XML like this: <root> <do-not-sort> <z/> <y/> </do-not-sort> <sortable> <e f=fog

  • 0

I have some XML like this:

<root>
   <do-not-sort>
      <z/>
      <y/>
   </do-not-sort>
   <sortable>
      <e f="fog" h="bat" j="cat">
          <n n="p"/>
          <m n="p"/>
      </e>
      <d b="fop" c="bar" k="cab">
          <m o="p"/>
          <m n="p"/>
      </d>
   </sortable>
</root>

I want to sort the children of the “sortable” element by their textual representation, to end up with this:

<root>
   <do-not-sort>
      <z/>
      <y/>
   </do-not-sort>
   <sortable>
      <d b="fop" c="bar" k="cab">
          <m n="p"/>
          <m o="p"/>
      </d>
      <e f="fog" h="bat" j="cat">
          <m n="p"/>
          <n n="p"/>
      </e>
   </sortable>
</root>

I am currently doing this by applying the following XSLT template:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes" encoding="UTF-8" />

  <xsl:template match="@* | /">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:value-of select="normalize-space(text()[1])" />
      <xsl:apply-templates select="*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="sortable//*">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:value-of select="normalize-space(text()[1])" />
      <xsl:apply-templates select="*">
        <xsl:sort data-type="text" select="local-name()" />
        <xsl:sort data-type="text" select="@*" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

The sorting works correctly, but if the sorted elements have a lot of attributes, the later attributes each wrap onto a new line, for example:

<sortable>
    <this is="an" element="with" a="lot" of="attributes"
            and="the"
            excess="ones"
            each="wrap"
            onto="their"
            own="line"/>

How do I keep all these attributes on the same line, i.e.

<sortable>
    <this is="an" element="with" a="lot" of="attributes" and="the" excess="ones" each="wrap" onto="their" own="line"/>
  • 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-15T19:41:00+00:00Added an answer on May 15, 2026 at 7:41 pm

    How do I keep all these attributes on
    the same line

    In your code, replace:

      <xsl:output method="xml" indent="yes" encoding="UTF-8" /> 
    

    with

      <xsl:output method="xml" encoding="UTF-8" /> 
    

    Of course, this will produce the complete output on a single line! At the moment of writing this XSLT 2.0 still doesn’t have a finer grained control over the serialization of the output.

    In case you need some elements indented and some not, then you will have to provide your own post-processing (and this post-processing may be easier to write with something different from XSLT).

    Update:

    Actually, using Saxon 9.1.07 and

      <xsl:output method="html" encoding="UTF-8" />
    

    where the complete transformation is:

    <xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:output method="html" encoding="UTF-8" />
    
      <xsl:template match="@* | /">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="*">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:value-of select="normalize-space(text()[1])" />
          <xsl:apply-templates select="*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="sortable//*">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:value-of select="normalize-space(text()[1])" />
          <xsl:apply-templates select="*">
            <xsl:sort data-type="text" select="local-name()" />
            <xsl:sort data-type="text" select="@*" />
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    and the source XML document is:

    <root>
       <do-not-sort>
          <z></z>
          <y></y>
       </do-not-sort>
       <sortable>
          <this is="an" element="with" a="lot" of="attributes" and="the" excess="ones" each="wrap" onto="their" own="line"></this>
          <e f="fog" h="bat" j="cat"></e>
          <d b="fop" c="bar" k="cab"></d>
          <d b="foo" c="baz" k="cap"></d>
       </sortable>
    </root>
    

    I get the output with the wanted indentation:

    <root>
       <do-not-sort>
          <z></z>
          <y></y>
       </do-not-sort>
       <sortable>
          <this is="an" element="with" a="lot" of="attributes" and="the" excess="ones" each="wrap" onto="their" own="line"></this>
          <e f="fog" h="bat" j="cat"></e>
          <d b="fop" c="bar" k="cab"></d>
          <d b="foo" c="baz" k="cap"></d>
       </sortable>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your description sounds a lot like the thing I wrote… May 16, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer Nice Link 4 implementing Lucene(Hibernate Search) http://www.amin-mc.blogspot.com/ Using Example of… May 16, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer I don't think it would make any difference in performance,… May 16, 2026 at 7:05 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.