Domanda

Hi i have an requirement to translate/convert/replace

'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ''1234567891011121314151617181920212223242526' 
first i am translating
 <xsl:variable name="lowercase2uppercase"
            select="translate($mystring,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

here i need to translate those 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' to '1234567891011121314151617181920212223242526'. for example letter 'A', 'B' to 'Z'i am translating like this

<xsl:when test="$letter='A'">
    <xsl:attribute name="letter">01</xsl:attribute>
</xsl:when>
<xsl:when test="$letter='B'">
    <xsl:attribute name="letter">02</xsl:attribute>
</xsl:when>
<xsl:when test="$letter='j'">
    <xsl:attribute name="letter">10</xsl:attribute>
</xsl:when>
.........## Heading ##
<xsl:when test="$letter='Z'">
    <xsl:attribute name="letter">26</xsl:attribute>
</xsl:when>

here , i can do like this but the solution is too lengthy, i need a generic solution to replace [A-Z] with [1-26] using XSLT. please help me . If my $letter is 'j' it is giving 00 instead 10 likewise for 't' also it is giving 10 instead of 20.

È stato utile?

Soluzione

How about using this function?

normalize-space(concat(
                translate($letter, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                                   '00000000011111111112222222'), 
                translate($letter, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                                   '12345678901234567890123456')))

It's still pretty concise and it does what you want. :-)

Altri suggerimenti

Well, if you define a variable, like so...

<xsl:variable name="replace" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

Then you could simplify the 'translation' of one letter, like so

<xsl:value-of select="string-length(substring-before($replace, $letter)) + 1" />

And to do it on a whole string, you would use a recursive template, where you replaced one letter at a time.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="replace" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

  <xsl:template match="word" >
    <xsl:copy>
       <xsl:call-template name="translate">
       <xsl:with-param name="text" select="." />
       </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="translate">
    <xsl:param name="text" />
    <xsl:if test="$text != ''">
      <xsl:variable name="letter" select="substring($text, 1, 1)" />
      <xsl:value-of select="string-length(substring-before($replace, $letter)) + 1" />
      <xsl:call-template name="translate">
        <xsl:with-param name="text" select="substring($text, 2, string-length($text) -1 )" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

When applied to this XML

<word>ABCDEFGHIJKLMNOPQRSTUVWXYZ</word>

The following is output

<word>1234567891011121314151617181920212223242526</word>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top