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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:20:58+00:00 2026-05-11T15:20:58+00:00

Based on the following XML, what is the best way to achieve an alphanumeric

  • 0

Based on the following XML, what is the best way to achieve an alphanumeric sort in XSL?

Edit: to clarify, the XML below is just a simple sample the real XML would contain much more variant values.

<colors>   <item>     <label>Yellow 100</label>   </item>   <item>     <label>Blue 12</label>   </item>   <item>     <label>Orange 3</label>   </item>   <item>     <label>Yellow 10</label>   </item>   <item>     <label>Orange 26</label>   </item>   <item>     <label>Blue 117</label>   </item> </colors> 

E.g. I want a final outcome in this order:

Blue 12, Blue 117, Orange 3, Orange 26, Yellow 10, Yellow 100 

This is ‘effectively‘ what I would want.

<xsl:apply-templates select='colors/item'>   <xsl:sort select='label' data-type='text' order='ascending'/><!--1st sort-->   <xsl:sort select='label' data-type='number' order='ascending'/><!--2nd sort--> </xsl:apply-templates>     <xsl:template match='item'>   <xsl:value-of select='label'/>   <xsl:if test='position() != last()'>,</xsl:if> </xsl:template> 
  • 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-11T15:20:59+00:00Added an answer on May 11, 2026 at 3:20 pm

    Splitting the label text into text and number part using substring-before and substring-after will do in your example (however, this is not a general approach, but you get the idea):

    <xsl:template match='/'>     <xsl:apply-templates select='colors/item'>       <xsl:sort select='substring-before(label, ' ')' data-type='text' order='ascending'/>       <!--1st sort-->       <xsl:sort select='substring-after(label, ' ')' data-type='number' order='ascending'/>       <!--2nd sort-->     </xsl:apply-templates>   </xsl:template>    <xsl:template match='item'>     <xsl:value-of select='label'/>     <xsl:if test='position() != last()'>, </xsl:if>   </xsl:template> 

    This gives the following output:

    Blue 12, Blue 117, Orange 3, Orange 26, Yellow 10, Yellow 100 

    Update

    A more generic way to solve your sorting problem would be to have the select attribute of the xls:sort element contain a string which is sortable according to the sort rules that you expect. E.g. in this string all numbers could be padded with leading 0’s so that lexicgraphically sorting them as data-type='text' will result in the correct alphanumeric order.

    If you are using the XSLT engine of .NET you could use a simple extension function in C# to pad numbers with leading 0’s:

    <?xml version='1.0' encoding='utf-8'?> <xsl:stylesheet version='1.0'                 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'                 xmlns:myExt='urn:myExtension'                 xmlns:msxsl='urn:schemas-microsoft-com:xslt'                 exclude-result-prefixes='msxsl myExt'>   <xsl:output method='xml' indent='yes' />    <msxsl:script language='C#' implements-prefix='myExt'>      <![CDATA[         private static string PadMatch(Match match)         {             // pad numbers with zeros to a maximum length of the largest int value              int maxLength = int.MaxValue.ToString().Length;             return match.Value.PadLeft(maxLength, '0');         }          public string padNumbers(string text)         {             return System.Text.RegularExpressions.Regex.Replace(text, '[0-9]+', new System.Text.RegularExpressions.MatchEvaluator(PadMatch));         }     ]]>    </msxsl:script>   <xsl:template match='/'>     <sorted>       <xsl:apply-templates select='colors/item'>         <xsl:sort select='myExt:padNumbers(label)' data-type='text' order='ascending'/>       </xsl:apply-templates>     </sorted>   </xsl:template>    <xsl:template match='item'>     <xsl:value-of select='label'/>     <xsl:if test='position() != last()'>, </xsl:if>   </xsl:template>  </xsl:stylesheet> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering what would be the best way to grab the following data and
Is it possible to convert the following XML configuration to an annotation based one?
I get the following text as a string from an XML-based REST API 'd':4
I want to get the Category based total of the following <?xml version=1.0 encoding=utf-8
Based on the following simple program the bitwise left shift operator works only for
Given the following XML and XSL transformation, I'd expect two <node/> elements in the
I want to modify my xml file in PHP based on the following criteria.
My SAAJ-based client generates the following XML and sends it to a .NET web
I'm trying my very best to edit the Magento design the way they want
I need to split the following XML file based on a predetermined value, for

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.