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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:48:58+00:00 2026-05-12T08:48:58+00:00

Is it possible in XSLT to sort in alphabetical order, with 5 items as

  • 0

Is it possible in XSLT to sort in alphabetical order, with 5 items as “preferred”.

i.e. given

<teams>
<team id="142" name="Scotland" />
<team id="110" name="Liverpool" />
<team id="13" name="Manchester United" />
<team id="123" name="England" />
<team id="84" name="Chelsea" />
<team id="295" name="Wales" />
<team id="49" name="Arsenal" />
<team id="126" name="Northern Ireland" />
<team id="121" name="Republic of Ireland" />
<team id="42" name="Manchester City" />
<team id="298" name="Tottenham Hotspur" />
<team id="299" name="Bolton" />
</teams>

I require the national teams to be sorted first in a certain order, followed by the rest in alphabetical order:

<teams>
<team id="123" name="England" />
<team id="126" name="Northern Ireland" />
<team id="121" name="Republic of Ireland" />
<team id="142" name="Scotland" />
<team id="295" name="Wales" />
<team id="49" name="Arsenal" />
<team id="299" name="Bolton" />
<team id="84" name="Chelsea" />
<team id="110" name="Liverpool" />
<team id="42" name="Manchester City" />
<team id="13" name="Manchester United" />
<team id="298" name="Tottenham Hotspur" />
</teams>

I have been trying, but failing.

Is there a neat way to do this, or do you have to sort the national teams individually, followed by a sort which excludes all the national teams?

  • 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-12T08:48:58+00:00Added an answer on May 12, 2026 at 8:48 am

    You could do this:

    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:my="http://tempuri.org"
      exclude-result-prefixes="my"
    >
    
      <xsl:output method="xml" indent="yes" /> 
    
      <my:data>
        <my:nationalteams>
          <my:team id="121" /><!-- Republic of Ireland -->
          <my:team id="123" /><!-- England -->
          <my:team id="126" /><!-- Northern Ireland -->
          <my:team id="142" /><!-- Scotland -->
          <my:team id="295" /><!-- Wales -->
        </my:nationalteams>
      </my:data>
    
      <xsl:template match="teams">
        <xsl:copy>
          <xsl:variable name="national" select="
            document('')/*/my:data/my:nationalteams/my:team
          " />
          <!-- national teams preferred -->
          <xsl:apply-templates select="team[@id = $national/@id]">
            <xsl:sort select="@name" />
          </xsl:apply-templates>
          <!-- other teams after them -->
          <xsl:apply-templates select="team[not(@id = $national/@id)]">
            <xsl:sort select="@name" />
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="team">
        <xsl:copy-of select="." />
      </xsl:template>
    
    </xsl:stylesheet>
    

    The whole <my:data> could be moved to a secondary XML/config file, where you can also leave off the “my” namespace.

    After that, one line would need a small change:

    <xsl:variable name="national" select="
      document('config.xml')/data/nationalteams/team
    " />
    

    The output of the above is somewhat unsurprising 🙂

    <teams>
      <team id="123" name="England" />
      <team id="126" name="Northern Ireland" />
      <team id="121" name="Republic of Ireland" />
      <team id="142" name="Scotland" />
      <team id="295" name="Wales" />
      <team id="49" name="Arsenal" />
      <team id="299" name="Bolton" />
      <team id="84" name="Chelsea" />
      <team id="110" name="Liverpool" />
      <team id="42" name="Manchester City" />
      <team id="13" name="Manchester United" />
      <team id="298" name="Tottenham Hotspur" />
    </teams>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering whether XSLT makes it possible to sort an XML file if
Is it possible in XSLT-1.0 to sort by a value in the xml. In
Is it possible using XSLT to group items in an XML file? Input file:
Possible Duplicate: Will XPath 2.0 and/or XSLT 2.0 be implemented in PHP? I think
Is it possible to get in Visual Studio code intellisense into XSLT extension functions.
Is it possible for me to have a conditional in XSLT such that I
Is it possible to show line numbers in the Developer Center when editing xslt
I'm fishing for approaches to a problem with XSLT processing. Is it possible to
Is it possible to use XSLT to transform XML into something other than XML?
Possible Duplicate: Interpreting newlines with XSLT xsl:text? How to convert newline into <br/> with

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.