2009-07-29 13 views
5

Domanda abbastanza semplice, come posso trasformare un numero (1, 2, 3, ecc.) in un numero ordinale stampabile (1 °, 2 °, 3 ° ecc.) usando xslt?Come trasformare un numero (1,2,3, ecc.) In un numero ordinale (1 °, 2 °, 3 ° ecc.) Usando xslt

Attualmente i seguenti lavori per 1-20, ma si può essere visto più grandi insiemi di entità ottenere classificato presto:

<xsl:template name="FormatRanking"> 
    <xsl:param name="Value"></xsl:param> 

    <xsl:choose> 
     <xsl:when test="$Value = '1'"> 
      <xsl:value-of select="$Value"/>st 
     </xsl:when> 
     <xsl:when test="$Value = '2'"> 
      <xsl:value-of select="$Value"/>nd 
     </xsl:when> 
     <xsl:when test="$Value = '3'"> 
      <xsl:value-of select="$Value"/>rd 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$Value"/>th 
     </xsl:otherwise> 
    </xsl:choose> 

</xsl:template> 

L'unico modo vorrei sapere come fare questo sarebbe quello di cambiare il xsl: quando di :

<xsl:when test="$Value = '1'"> 
<xsl:when test="$Value = '2'"> 
<xsl:when test="$Value = '3'"> 

a (non sono nemmeno sicuro se questo è corretto):

<xsl:when test="$Value = '1' or $Value = '21' or $Value = '31' ..."> 
<xsl:when test="$Value = '2' or $Value = '22' or $Value = '33' ..."> 
<xsl:when test="$Value = '3' or $Value = '22' or $Value = '33' ..."> 

mi piacerebbe farlo mething simile a questo Is there an easy way to create ordinals in C#? ma non sono sicuro che sia possibile in Xslt.

A questo punto abbiamo solo bisogno di una soluzione inglese.

+2

La tua fiducia nelle capacità di XSLT è commovente, se selvaggiamente non realistico – skaffman

+1

@skaffman: Immagino tu stia sottovalutando le capacità di XSLT. – Tomalak

+0

Dopotutto, XSLT dovrebbe essere completato. – Eric

risposta

9

Ecco la soluzione da "Is there an easy way to create ordinals in C#?", tradotta in XSLT:

<xsl:template name="FormatRanking"> 
    <xsl:param name="Value" select="0" /> 

    <xsl:value-of select="$Value"/> 

    <!-- a little parameter sanity check (integer > 0) --> 
    <xsl:if test=" 
    translate($Value, '', '') = '' 
    and 
    $Value > 0 
    "> 
    <xsl:variable name="mod100" select="$Value mod 100" /> 
    <xsl:variable name="mod10" select="$Value mod 10" /> 

    <xsl:choose> 
     <xsl:when test="$mod100 = 11 or $mod100 = 12 or $mod100 = 13">th</xsl:when> 
     <xsl:when test="$mod10 = 1">st</xsl:when> 
     <xsl:when test="$mod10 = 2">nd</xsl:when> 
     <xsl:when test="$mod10 = 3">rd</xsl:when> 
     <xsl:otherwise>th</xsl:otherwise> 
    </xsl:choose> 
    </xsl:if> 
</xsl:template> 
+0

Grazie Tomalak, questo è esattamente ciò di cui ho bisogno! –

+0

@Tomalak Grazie mille !!! Questo è qualcosa che stavo cercando. Sei un genio :) – DashaLuna

0

Potresti utilizzare un'estensione XSLT? So che EXSLT ha una vasta gamma di date and time extensions.

+0

Non sono nemmeno sicuro di cosa sia un'estensione XSLT e non ho tempo per esaminarlo, grazie per il suggerimento. Prenderò un appunto per cercarlo più tardi. –

+0

@Dan Ho guardato le estensioni di data e ora EXSLT, non sembra che abbiano una soluzione/estensione per questa attività. – DashaLuna

5

Non sto dicendo che sia una buona idea per fare questo in XSLT, ma ...

<xsl:template name="FormatRanking"> 
    <xsl:param name="Value"></xsl:param> 

    <xsl:choose> 
      <xsl:when test="substring($Value,string-length($Value)-1) = '11'"> 
        <xsl:value-of select="$Value"/>th 
      </xsl:when> 
      <xsl:when test="substring($Value,string-length($Value)-1) = '12'"> 
        <xsl:value-of select="$Value"/>th 
      </xsl:when> 
      <xsl:when test="substring($Value,string-length($Value)-1) = '13'"> 
        <xsl:value-of select="$Value"/>th 
      </xsl:when> 
      <xsl:when test="substring($Value,string-length($Value)) = '1'"> 
        <xsl:value-of select="$Value"/>st 
      </xsl:when> 
      <xsl:when test="substring($Value,string-length($Value)) = '2'"> 
        <xsl:value-of select="$Value"/>nd 
      </xsl:when> 
      <xsl:when test="substring($Value,string-length($Value)) = '3'"> 
        <xsl:value-of select="$Value"/>rd 
      </xsl:when> 
      <xsl:otherwise> 
        <xsl:value-of select="$Value"/>th 
      </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

Edit: o un po 'più ordinato soluzione:

<xsl:template name="FormatRanking"> 
    <xsl:param name="Value"></xsl:param> 

    <xsl:variable name="penultimateChar" select="substring($Value,string-length($Value)-1, 1)"/> 
    <xsl:variable name="lastChar" select="substring($Value,string-length($Value))"/> 

    <xsl:value-of select="$Value"/> 
    <xsl:choose> 
      <xsl:when test="$penultimateChar= '1'"> 
        <xsl:text>th </xsl:text> 
      </xsl:when> 
      <xsl:when test="$lastChar = '1'"> 
        <xsl:text>st </xsl:text> 
      </xsl:when> 
      <xsl:when test="$lastChar = '2'"> 
        <xsl:text>nd </xsl:text> 
      </xsl:when> 
      <xsl:when test="$lastChar = '3'"> 
        <xsl:text>rd </xsl:text> 
      </xsl:when> 
      <xsl:otherwise> 
        <xsl:text>th </xsl:text> 
      </xsl:otherwise> 
    </xsl:choose> 

+0

+1 per la soluzione # 2, questo è abbastanza vicino. Mi asterrò dall'applicare le funzioni di stringa ai numeri, tuttavia, purché ci sia un modo per farlo con le funzioni numeriche. – Tomalak

+0

Sì. buon punto. – Alohci

+0

Grazie per i suggerimenti Alohci, inizialmente pensavo di fare qualcosa come la soluzione n. 2 ma non sapevo quali funzioni di stringa usare. Comunque vado con la soluzione di Tomalak che rispecchia l'idea di C# e usa la matematica per farlo al posto delle funzioni di stringa. –

Problemi correlati