XSLT2.0で便利になった機能(12) 文字と文字コードの相互変換

先だって文字列の比較について紹介しましたが、XSLT2.0では文字列と整数(ユニコードのcodepoint値)の相互変換もできるようになりました.
xs:integer*からxs:stringへは、codepoints-to-string()
xs:string?からxs:integer*へは、string-to-codepoints()
の2つの関数です.
 
たとえば、string-to-codepoints('A') は 65 を返します.逆に codepoints-to-string(65) は、"A"を返します.
 
また一例として私が携わった仕事で、「部」-「章」-「節」で構成される文書があり、部は"A","B","C"...で表すという仕様がありました.部の番号から"A","B","C"...へ変換しなければならないのですが.XSLT1.0では例のごとくで、仕方なく
 
<xsl:template name="partNumberToAlpha">
    <xsl:param name="prmPartNumber" select="1"/>
    <xsl:choose>
        <xsl:when test="$prmPartNumber=1"><xsl:value-of select="'A'"/></xsl:when>
        <xsl:when test="$prmPartNumber=2"><xsl:value-of select="'B'"/></xsl:when>
        <xsl:when test="$prmPartNumber=3"><xsl:value-of select="'C'"/></xsl:when>
        ...
        <xsl:when test="$prmPartNumber=24"><xsl:value-of select="'X'"/></xsl:when>
        <xsl:when test="$prmPartNumber=25"><xsl:value-of select="'Y'"/></xsl:when>
        <xsl:when test="$prmPartNumber=26"><xsl:value-of select="'Z'"/></xsl:when>
        <xsl:otherwise><xsl:value-of select="'?'"/></xsl:otherwise>
    </xsl:choose>
</xsl:template>
 
とやっていました.でもXSLT2.0なら、この相互変換の関数が使えます.
 
<xsl:function name="tmf:partNumberToAlpha">
    <xsl:param name="prmPartNumber" as="xs:integer"/>
    <xsl:choose>
       <xsl:when test="$prmPartNumber le (string-to-codepoints('Z') - string-to-codepoints('A') + 1)">
            <xsl:sequence select="codepoints-to-string(string-to-codepoints('A') + $prmPartNumber -1)"/>
       </xsl:when>
       <xsl:otherwise>
            <xsl:sequence select="'?'"/>
       </xsl:otherwise>
    </xsl:choose>
</xsl:function>
 
ちょっと使いすぎでしょうか?
しかしとにもかくにも文字と文字コードの変換ができるようになったことはあたりまえですがすばらしいことです.きっとあってよかったと思うときがあるでしょう.