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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:08:53+00:00 2026-05-11T06:08:53+00:00

I have an input XML file which I need to copy 1:1 to the

  • 0

I have an input XML file which I need to copy 1:1 to the output, except for one subelement which contains subitems that need to be sorted.

<?xml version='1.0'?> <top>   <elementA />   <elementB />   <contents>       <contentitem>           <id>3</id>           <moretags1 />           <moretags2 />       </contentitem>       <contentitem>           <id>2</id>           <moretags1 />           <moretags2 />       </contentitem>       <contentitem>           <id>1</id>           <moretags1 />           <moretags2 />       </contentitem>   </contents> </top> 

I’d like an XSL Transformation that puts the ‘contentitem’ elements in order, sorted by their ‘id’ elements. All other tags, including nested ones, must be copied verbatim. I already tried with xsl:copy, but either I get double contents or something turns out missing.

  • 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. 2026-05-11T06:08:54+00:00Added an answer on May 11, 2026 at 6:08 am

    Mark Gravell’s solution is almost correct — with a slight issue that creates two nested <contents> elements. Note to all who provide answers: Do test your solutions!

    Here is a correct solution. This transformation:

    <xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>  <xsl:output omit-xml-declaration='yes' indent='yes'/> <!--                                                -->   <xsl:strip-space elements='*'/> <!--                                                -->    <xsl:template match='node()|@*'>     <xsl:copy>       <xsl:apply-templates select='node()|@*'/>     </xsl:copy>   </xsl:template> <!--                                                -->    <xsl:template match='contents'>     <xsl:copy>       <xsl:apply-templates select='@*'/>       <xsl:apply-templates select='contentitem'>         <xsl:sort select='id' data-type='number'/>       </xsl:apply-templates>     </xsl:copy>   </xsl:template> </xsl:stylesheet> 

    when applied on the originally-provided XML document:

    <top>     <elementA />     <elementB />     <contents>         <contentitem>             <id>3</id>             <moretags1 />             <moretags2 />         </contentitem>         <contentitem>             <id>2</id>             <moretags1 />             <moretags2 />         </contentitem>         <contentitem>             <id>1</id>             <moretags1 />             <moretags2 />         </contentitem>     </contents> </top> 

    produces the wanted, correct result:

    <top>    <elementA/>    <elementB/>    <contents>       <contentitem>          <id>1</id>          <moretags1/>          <moretags2/>       </contentitem>       <contentitem>          <id>2</id>          <moretags1/>          <moretags2/>       </contentitem>       <contentitem>          <id>3</id>          <moretags1/>          <moretags2/>       </contentitem>    </contents> </top> 

    Do note the following:

    1. The use of the identity rule to copy all nodes without change.

    2. How the identity template is overriden with a specific template matching the contents element

    3. The use of the <xsl:sort> instruction to present the results of applying a template in a specific order, possibly different from the document order of the nodes, selected for processing.

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

Sidebar

Related Questions

I have an XML input file and I'm trying to output the result of
I have an input file that I want to sort based on timestamp which
I have rather large input XML files that must be converted to a given
i have a input tag which is non editable, but some times i need
We have some input data that sometimes appears with &nbsp characters on the end.
I have an input which at some points happens to have the focus. If
Question We have a large number of xml configuration files that we want merged
I have a list of wild card based patter in a XML file. If
I've got a Perl script which consumes an XML file on Linux and occasionally
The problem I have is that I need to do about 40+ conversions to

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.