<xsl:number> 的自定义格式

ewh*_*ewh 5 xslt saxon xslt-2.0

是否可以定义自定义格式<xsl:number>

我有这样的情况:需要标准的基于字母的格式,但字母表中的某些字符是被禁止的(奇怪的要求,但这是客户所要求的)。例如,字母i不能使用,所以使用时<xsl:number>我应该得到序列:a, b, c, d, e, f, g, h, j, k, ..., aa, ab, ...,啊,阿杰,...

该项目使用 XSLT 2.0 和 Saxon,因此如果存在特定于 Saxon 的解决方案,那就没问题。

XSLT 2.0 是否提供定义自定义格式序列的功能?Saxon 是否提供注册自定义序列以供使用的功能<xsl:number>

ewh*_*ewh 1

在发布我对问题的原始解决方案后,我提出了以下更通用的解决方案。该解决方案是纯 XSLT,并且在基础上仍然使用<xsl:number>,因此应该适用于任何格式类型。

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:ewh="http://www.earlhood.com/XSL/Transform"
                exclude-result-prefixes="#all">

<!-- Description: XSLT to generate a alpha formatted sequence
     label (via <xsl:number>), but disallowing specific characters
     from being used.
  -->

<!-- Algorithm: Given the index value of the item to generate
     a label for via <xsl:number>, we adjust the value so the resulting
     label avoids the use of the forbidden characters.

     This is achieved by converting the index value into a baseX
     number, with X the number of allowed characters.

     The baseX number will be converted into a reverse sequence
     of numbers for each ^E place.  For example, the number 12167
     converted to base23 will generate the following reverse sequence:

       Place:    (23^0, 23^1, 23^2, 23^3)
       Sequence: (   0,    0,    0,    1)   // 1000 in base23

     Having it in right-to-left order makes processing easier.

     Each item in the sequence will be a number from 0 to baseX-1.

     With the sequence, we can then just call <xsl:number> on
     each item and reverse concatenate the result.

     NOTE: Since <xsl:number> does not like 0 as a given value,
     the sequence must be processed so each item is within the
     range of 1-to-baseX.  For example, the above base23 example
     will be translated to the following:

       (23, 22, 22)
  -->

<xsl:output method="xml" indent="yes"/>

<!-- Number of allowed characters: This should be total number of chars of
     format-type desired minus the chars that should be skipped. -->
<xsl:variable name="lbase" select="23"/>
<!-- Sequence of character positions not allowed, with 1=>a to 26=>z -->
<xsl:variable name="lexcs" select="(9,12,15)"/> <!-- i,l,o -->

<!-- Helper Function:
     Convert integer to sequence of number of given base.
     The sequence of numbers is in reverse order: ^0,^1,^2,...^N.
  -->
<xsl:function name="ewh:get_base_digits" as="item()*">
  <xsl:param name="number" as="xs:integer"/>
  <xsl:param name="to"     as="xs:integer"/>
  <xsl:variable name="Q" select="$number idiv $to"/>
  <xsl:variable name="R" select="$number mod $to"/>
  <xsl:sequence select="$R"/>
  <xsl:if test="$Q gt 0">
    <xsl:sequence select="ewh:get_base_digits($Q,$to)"/>
  </xsl:if>
</xsl:function>

<!-- Helper Function:
     Compute carry-overs in reverse-base digit sequence.  XSLT starts
     numbering at 1, so we cannot have any 0s.
  -->
<xsl:function name="ewh:compute_carry_overs" as="item()*">
  <xsl:param name="digits" as="item()*"/>
  <xsl:variable name="d" select="subsequence($digits, 1, 1)"/>
  <xsl:choose>
    <xsl:when test="($d le 0) and (count($digits) = 1)">
      <!-- 0 at end of list, nothing to do -->
    </xsl:when>
    <xsl:when test="$d le 0">
      <!-- If digit <=0, need to perform carry-over operation -->
      <xsl:variable name="next" select="subsequence($digits, 2, 1)"/>
      <xsl:choose>
        <xsl:when test="count($digits) le 2">
          <xsl:sequence select="$lbase + $d"/>
          <xsl:sequence select="ewh:compute_carry_overs($next - 1)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:sequence select="$lbase + $d"/>
          <xsl:sequence select="ewh:compute_carry_overs(($next - 1,
              subsequence($digits, 3)))"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="count($digits) le 1">
      <xsl:sequence select="$d"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="$d"/>
      <xsl:sequence select="ewh:compute_carry_overs(subsequence($digits, 2))"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<!-- Helper Function:
     Given a number in the base range, determine number for
     purposes of <xsl:number>.  We loop thru the exclusion
     list and add 1 for each exclusion letter that has
     been passed.  The $digit parameter should be a number
     in the range [1..$lbase].
  -->
<xsl:function name="ewh:compute_digit_offset" as="xs:integer">
  <xsl:param name="digit"      as="xs:integer"/>
  <xsl:param name="excludes"   as="item()*"/>
  <xsl:variable name="l" select="subsequence($excludes, 1, 1)"/>
  <xsl:variable name="result">
    <xsl:choose>
      <xsl:when test="$digit lt $l">
        <xsl:value-of select="0"/>
      </xsl:when>
      <xsl:when test="count($excludes) = 1">
        <xsl:value-of select="1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="rest">
          <xsl:value-of select="ewh:compute_digit_offset($digit+1,
              subsequence($excludes,2))"/>
        </xsl:variable>
        <xsl:value-of select="1 + $rest"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="$result"/>
</xsl:function>

<!-- Retrieve alpha sequence label.
     This is the main function to call.
  -->
<xsl:function name="ewh:get-alpha-label" as="xs:string">
  <xsl:param name="number" as="xs:integer"/>
  <xsl:variable name="basedigits"
                select="ewh:get_base_digits($number,$lbase)"/>
  <xsl:variable name="digits"
                select="ewh:compute_carry_overs($basedigits)"/>
  <xsl:variable name="result" as="item()*">
    <xsl:for-each select="$digits">
      <xsl:variable name="digit" select="."/>
      <!-- Should not have any 0 values.  If some reason we do,
           we ignore assuming they are trailing items. -->
      <xsl:if test="$digit != 0">
        <xsl:variable name="value">
          <xsl:value-of select="$digit +
              ewh:compute_digit_offset($digit,$lexcs)"/>
        </xsl:variable>
        <xsl:variable name="number">
          <xsl:number value="$value" format="a"/>
        </xsl:variable>
        <xsl:sequence select="$number"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:value-of select="string-join(reverse($result),'')"/>
</xsl:function>

<!-- For testing -->
<xsl:template match="/">
  <result>
    <xsl:for-each select="(1 to 1000,12166,12167,12168,279840,279841,279842)">
      <value n="{.}"><xsl:value-of select="ewh:get-alpha-label(.)"/></value>
    </xsl:for-each>
  </result>
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)