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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:53:21+00:00 2026-05-12T16:53:21+00:00

I am working with XSLT 1.0 (so I can’t use the replace() function), and

  • 0

I am working with XSLT 1.0 (so I can’t use the replace() function), and I need to make a replace in a string, before use that string for sorting. Briefly, my XML doc looks like this:

<root>
    <item>
        <name>ABC</name>
        <rating>good</rating>
    </item>
    <item>
        <name>BCD</name>
        <rating>3</rating>
    </item>
</root>

Then I need to replace ‘good’ with ‘4’, in order to print the whole items list ordered by rating using the sort() function. Since I’m using XSLT 1.0, I use this template for replacements:

<xsl:template name="string-replace">
  <xsl:param name="subject"     select="''" />
  <xsl:param name="search"      select="''" />
  <xsl:param name="replacement" select="''" />
  <xsl:param name="global"      select="false()" />

  <xsl:choose>
    <xsl:when test="contains($subject, $search)">
      <xsl:value-of select="substring-before($subject, $search)" />
      <xsl:value-of select="$replacement" />
      <xsl:variable name="rest" select="substring-after($subject, $search)" />
      <xsl:choose>
        <xsl:when test="$global">
          <xsl:call-template name="string-replace">
            <xsl:with-param name="subject"     select="$rest" />
            <xsl:with-param name="search"      select="$search" />
            <xsl:with-param name="replacement" select="$replacement" />
            <xsl:with-param name="global"      select="$global" />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$rest" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$subject" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

This templates works fine, but the problem is that it always print the values, (i.e. always when I call the template something is printed). Therefore, this template is not usefull in this case, because I need to modify the ‘rating’ value, then sort the items by rating and finally print them.

Thanks in advance!

PS: A workaround would be use two different XSLT, but for several reasons I can’t do it in this case.

  • 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-12T16:53:22+00:00Added an answer on May 12, 2026 at 4:53 pm

    You can do this:

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
      <xsl:output method="xml" encoding="utf-8" />
    
      <xsl:template match="/root">
        <xsl:for-each select="item">
          <!-- be sure to include every possible value of <rating>! -->
          <xsl:sort select="
            concat(
              substring('4', 1, rating = 'good' ),
              substring('3', 1, rating = 'medioce' ),
              substring('2', 1, rating = 'bad' ),
              substring('1', 1, rating = 'abyssmal' ),
              substring('4', 1, rating = '4' ),
              substring('3', 1, rating = '3' ),
              substring('2', 1, rating = '2' ),
              substring('1', 1, rating = '1' )
            )
          " order="descending" />
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>
    

    With an input of:

    <root>
      <item>
        <name>ABC</name>
        <rating>abyssmal</rating>
      </item>
      <item>
        <name>GEH</name>
        <rating>bad</rating>
      </item>
      <item>
        <name>DEF</name>
        <rating>good</rating>
      </item>
      <item>
        <name>IJK</name>
        <rating>medioce</rating>
      </item>
    </root>
    

    I get:

    <item>
      <name>DEF</name>
      <rating>good</rating>
    </item>
    <item>
      <name>IJK</name>
      <rating>medioce</rating>
    </item>
    <item>
      <name>GEH</name>
      <rating>bad</rating>
    </item>
    <item>
      <name>ABC</name>
      <rating>abyssmal</rating>
    </item>
    

    For an explanation, see my other answer. 😉


    EDIT

    Changed solution upon this comment of the OP:

    I need to use the rating (with the
    strings replaced by integer scores), 3
    times:

    1. make a key with <xsl:key ... using the rating
    2. Sort the items using the rating
    3. Print the rating.

    In each step I should use the rating
    AFTER the replace, (i.e. using integer
    scores). I have done it repeating the
    concat(...) code 3 times, but as you
    can see this is not too cool… I
    would like to find a way to place the
    concat (...) one time, without need to
    repeat it.

    The following XSLT 1.0 solution fulfills all these requests:

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:tmp="http://tempuri.org/"
      exclude-result-prefixes="tmp"
    >
      <xsl:output method="xml" encoding="utf-8" />
    
      <!-- prepare a list of possible ratings for iteration -->
      <tmp:ratings>
        <tmp:rating num="1" />
        <tmp:rating num="2" />
        <tmp:rating num="3" />
        <tmp:rating num="4" />
      </tmp:ratings>
    
      <!-- index items by their rating -->
      <xsl:key 
        name="kItemByRating" 
        match="item" 
        use="concat(
          substring('4', 1, rating = 'good' ),
          substring('3', 1, rating = 'medioce' ),
          substring('2', 1, rating = 'bad' ),
          substring('1', 1, rating = 'abyssmal' ),
          substring('4', 1, rating = '4' ),
          substring('3', 1, rating = '3' ),
          substring('2', 1, rating = '2' ),
          substring('1', 1, rating = '1' )
        )
      " />
    
      <!-- we're going to need that later-on -->
      <xsl:variable name="root" select="/" />
    
      <xsl:template match="/root">
        <!-- iterate on the prepared list of ratings -->
        <xsl:apply-templates select="document('')/*/tmp:ratings/tmp:rating">
          <xsl:sort select="@num" order="descending" />
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="tmp:rating">
        <xsl:variable name="num" select="@num" />
        <!-- 
          The context node is part of the XSL file now. As a consequence,
          a call to key() would be evaluated within the XSL file.
    
          The for-each is a means to change the context node back to the 
          XML file, so that the call to key() can return <item> nodes.
        -->
        <xsl:for-each select="$root">
          <!-- now pull out all items with a specific rating -->
          <xsl:apply-templates select="key('kItemByRating', $num)">
            <!-- note that we use the variable here! -->
            <xsl:with-param name="num" select="$num" />
            <xsl:sort select="@name" />
          </xsl:apply-templates>
        </xsl:for-each>
      </xsl:template>
    
      <xsl:template match="item">
        <xsl:param name="num" select="''" />
        <xsl:copy>
          <!-- print out the numeric rating -->
          <xsl:attribute name="num">
            <xsl:value-of select="$num" />
          </xsl:attribute>
          <xsl:copy-of select="node() | @*" />
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 191k
  • Answers 191k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: $url = "www.example.com/$link"; When string is in double… May 12, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer I've used Beanstalk in production, and also while testing threw… May 12, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer Since you aren't supplying the parameter to indicate whether the… May 12, 2026 at 6:13 pm

Related Questions

My job would be easier, or at least less tedious if I could come
A while ago, I started on a project where I designed a html-esque XML
When I have the following (default) declaration in my XSL file, everything works fine.
I am working on a project right now that involves receiving a message from
I am trying to create hyperlinks using XML information and XSLT templates. Here is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.