我想检查字符串以number
、period(.)
和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.
(开头,然后是.
和空格。此时,para/@type
必须是number
。您可以看到第一个和第二个<text>
元素通过了该条件。则@type
必须是number
。另一方面CCD_ 14。就像第三个<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>