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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:33:39+00:00 2026-06-09T05:33:39+00:00

Is it possible in XSLT-1.0 to sort by a value in the xml. In

  • 0

Is it possible in XSLT-1.0 to sort by a value in the xml. In the example below the <examples> has requested that that the list of <example>s be sorted by the year field.

...
<examples>
  <sortby>year</sortby>

  <example>
    <year>2012</year>
    <number>3</number>
  </example>

</examples>
...

A static conditional using choose does not work in this case as I will not know what the possible fields in <example> will be.

  • 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-09T05:33:40+00:00Added an answer on June 9, 2026 at 5:33 am

    Yes, use something of this form:

    <xsl:apply-templates select="example">
      <xsl:sort select="*[name()=sortby]" data-type="{sort-data-type}" 
                order="{sort-order}" />
    </xsl:apply-templates>
    

    Do note that you need to also specify the sort data-type and order.

    Complete example:

    <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="/*">
         <xsl:copy>
        <xsl:apply-templates select="example">
          <xsl:sort select="*[name()=/*/sortby]" data-type="{/*/sort-data-type}"
                    order="{/*/sort-order}" />
        </xsl:apply-templates>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="example">
      <xsl:copy-of select="."/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document:

    <examples>
        <sortby>year</sortby>
        <sort-data-type>number</sort-data-type>
        <sort-order>descending</sort-order>
        <example>
            <year>2008</year>
            <number>3</number>
        </example>
        <example>
            <year>2012</year>
            <number>2</number>
        </example>
        <example>
            <year>2010</year>
            <number>5</number>
        </example>
    </examples>
    

    the wanted, correct result is produced:

    <examples>
       <example>
          <year>2012</year>
          <number>2</number>
       </example>
       <example>
          <year>2010</year>
          <number>5</number>
       </example>
       <example>
          <year>2008</year>
          <number>3</number>
       </example>
    </examples>
    

    If the same transformation is applied on this XML document (the same as above, but with changed sortby and sort-order):

    <examples>
        <sortby>number</sortby>
        <sort-data-type>number</sort-data-type>
        <sort-order>ascending</sort-order>
        <example>
            <year>2008</year>
            <number>3</number>
        </example>
        <example>
            <year>2012</year>
            <number>2</number>
        </example>
        <example>
            <year>2010</year>
            <number>5</number>
        </example>
    </examples>
    

    then again the wanted, correct result is produced:

    <examples>
       <example>
          <year>2012</year>
          <number>2</number>
       </example>
       <example>
          <year>2008</year>
          <number>3</number>
       </example>
       <example>
          <year>2010</year>
          <number>5</number>
       </example>
    </examples>
    

    Explanation:

    1. The select attribute of xsl:sort can contain any XPath expression, so we can specify an expression that selects any child of the elements to be sorted, such that its name is the result of evaluating another XPath expression.

    2. In XSLT, typically, all attributes other than select allow *AVT*s (Attribute Value Templates) to be specified in their values.

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

Sidebar

Related Questions

Has this ever been done before? Is it possible to write an XSLT that
I am wondering whether XSLT makes it possible to sort an XML file if
Possible Duplicate: Add an attribute with static value with xslt I have an XML
Is it possible for me to have a conditional in XSLT such that I
Is it possible to use XSLT to transform XML into something other than XML?
This one I really think is not possible to solve through XSLT, so that
is it possible to send user and email in xml format that is formatted
Is it possible in XSLT to sort in alphabetical order, with 5 items as
Possible Duplicate: How can I make XSLT work in chrome? I have this XML
Is it possible to remove xml attributes from XSLT AND work with the resulting

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.