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 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 written xml file which contains html tags as element like <component> <input
I have a XML file having default namespace and empty namespaces which need to
I have an XML file which describes the data-structure that I can exchange on
I have this use case of an xml file with input like Input: <abc
I have an XML file which is structured as follows: <row> <store>place1</store> <location>location1</location> </row>
I need a way to include an external XML file in PHP which does
I have a application, which accepts xml files, upon uploading a invalid xml file
I need to write an HTML page that has multiple input fields in which
I have a pipe delimited text file as shown below, which I need to
I have the following Perl code, which reads the input and indents the file

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.