检查字符串如何在 XSLT 中以数字、句点 (.) 和 space 开头

How check string starts with number, period(.) and space in XSLT

我想检查以 numberperiod(.)space 开头的字符串。为此,我使用了 regex 但没有给出正确答案。

输入:

<para>
    <text>1. this a paragaraph1</text>
    <text>12. this a paragaraph2</text>
    <text>this a paragaraph3</text>
</para>

输出应该是:

<result>
    <para type="number">1. this a paragaraph1</para>
    <para type="number">12. this a paragaraph2</para>
    <para type="not number">this a paragaraph3</para>
</result>

逻辑解释:

可以看到para/text有一个字符串。其中一些以数字开头(1. 12. ),然后是 . 和 space。那时候para/@type一定是number。您可以看到第一个和第二个 <text> 元素通过了该条件。那么那个@type一定是number。否则 not number。就像第 3 个 <text> 元素

尝试过的代码:

<xsl:template match="text">
    <xsl:choose>
        <xsl:when test="matches(.,'[[0-9]+]')">
            <para type="number">
                <xsl:value-of select="."/>
            </para>
        </xsl:when>
        <xsl:otherwise>
            <para type="not number">
                <xsl:value-of select="."/>
            </para>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
    
<xsl:template match="para">
    <xsl:apply-templates/>
</xsl:template>

使用 ^ 将正则表达式锚定在字符串的开头,并使用更简单的语法,例如

<xsl:template match="text[matches(., '^[0-9]+')]">
  <para type="number">
    <xsl:apply-templates/>
  </para>
</xsl:template>