0

I have the following String, which represents days of the week:

1, 2, 3, 4, 5

I'd like to replace those values with:

M, Tu, W, ...

I don't know that there is an XSLT 2.0 function that will handle that in one shot. Does anyone know of a way to accomplish this?

Thanks

4
  • What version of XSLT? Commented Nov 25, 2014 at 17:43
  • Not one function, but can't you do it with nested replace's, one for each day of the week? Actually you can do M, W, F with a translate so you just need two replaces to deal with Tu and Th (and Sa, Su, if you're dealing with weekends too). Commented Nov 25, 2014 at 17:44
  • Also, what is the context? Is the string the value of an element or attribute? Is it always 1, 2, 3, 4, 5 or does it vary? Commented Nov 25, 2014 at 17:47
  • XSLT 2.0. The string is a node value, and can be a combination of any of those values: "1, 3" or "2, 4, 5" or "2", ... I'm grabbing it from the node, and want to convert to "M, Tu, ..." for display on the page. Commented Nov 25, 2014 at 18:09

2 Answers 2

3

Similar to C. M. Sperberg-McQueen's answer only using a sequence as the variable...

XML Input

<doc>
    <x>1, 2, 3, 4, 5</x>
    <x>1, 3, 5</x>
    <x>2, 4</x>
</doc>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="daysOfWeek" select="('M','Tu','W','Th','F','Sa','Su')"/>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="x">
        <x>
            <xsl:value-of select="for $n in tokenize(.,',') 
                return $daysOfWeek[position()=number(normalize-space($n))]" 
                separator=", "/>        
        </x>
    </xsl:template>

</xsl:stylesheet>

XML Output

<doc>
   <x>M, Tu, W, Th, F</x>
   <x>M, W, F</x>
   <x>Tu, Th</x>
</doc>
Sign up to request clarification or add additional context in comments.

Comments

1

No, there is no built-in function in XSLT that will take a comma-delimited string with numerals in the range 1-7 (or 0-6) and return a comma-delimited sequence of the corresponding one- or two-character abbreviations of the days of the week. You'll have to use more than one function call.

Assuming for simplicity that you're in XSLT 2.0:

<xsl:variable name="daynames" as="element(day)*">
  <day n="1">M</day>
  <day n="2">Tu</day>
  <day n="3">W</day>
  <day n="4">Th</day>
  <day n="5">F</day>
  <day n="6">Sa</day>
  <day n="7">Su</day>
</

<xsl:variable name="string" value="'1, 2, 3, 4, 5'"/>

<xsl:value-of select="string-join(
  for $n in tokenize($string,', ') return $days[@n=$n]/string(),
  ', ')"/>

In XSLT 1.0, this will be a little more verbose, but can be done with a recursive named template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.