匹配基于范围的字母与字母表

时间:2015-05-25 09:00:18

标签: xslt xslt-2.0

我有以下的XML。

<para>39B</para>
<para>76</para>

在这里,我能够匹配确切的数字,但是当出现如下所述的范围时,问题出现了。

if the range 39A-39P
  print case1
if the range 72-85
  print case2

我试过的代码

  <xsl:template match="text()">
    <xsl:analyze-string select="." regex="([\w]+)">
      <xsl:matching-substring>
        <xsl:variable name="regex1">
          <xsl:choose>
            <xsl:when test="contains(regex-group(1), '^39[A-P]$')">
              <xsl:text>CAse 1</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>Case 2</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:value-of select = "$regex1"/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>

请让我知道如何解决这个问题。

由于

1 个答案:

答案 0 :(得分:2)

使用XSLT 2.0,您可以使用正则表达式,因此对于字母数字的情况,您可以使用

<xsl:template match="para[matches(., '^39[A-P]$')]">case1</xsl:template>

和第二种情况的简单数字比较

<xsl:template match="para[. ge 72 and . le 85]">case2</xsl:template>