XSLT - 正则表达式从 text() 节点中仅选择一位数字 [英] XSLT - regex select only one digit number from text() node

查看:23
本文介绍了XSLT - 正则表达式从 text() 节点中仅选择一位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 xml,

<section>
    <para>height 4cm, width 5cm, weight 343</para>
    <para>height 2cm, width 6cm, weight 410</para>
    <para>height 3cm, width 1cm, weight 590</para>
</section>

这里我需要将 para/text() 的个位数加倍.所需的输出应该是这样的,

here I need to doubled the single digit numbers of the para/text(). desired output should looks like,

  <section>
        <para>height 8cm, width 10cm, weight 343</para>
        <para>height 4cm, width 12cm, weight 410</para>
        <para>height 6cm, width 2cm, weight 590</para>
    </section>

为此,我有一个这样的模板,

To do this I've a template like this,

<xsl:template match="para/text()">
        <xsl:analyze-string select="." regex="\d">

            <xsl:matching-substring>
                <xsl:value-of select="2 * number(.)"/>
            </xsl:matching-substring>

            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>

        </xsl:analyze-string>
    </xsl:template>

这里的问题是这样的,这将没有一位数作为一位一位数并将它们加倍,

the problem here is this, this takes none single digit number as one digit by one and double them up,

电流输出,

<section>
    <para>height 8cm, width 10cm, weight 686</para>
    <para>height 4cm, width 12cm, weight 820</para>
    <para>height 6cm, width 2cm, weight 10180</para>
</section>

有什么建议可以解决这个问题吗?

any suggestion how can I solve this?

推荐答案

如果您将单位数"定义为由非数字字符包围的单数,则可以使用:

If you define a "single digit number" as a single digit surrounded by non-digit characters, you can then use:

<xsl:template match="para/text()">
    <xsl:analyze-string select="." regex="(\D)(\d)(\D)">

    <xsl:matching-substring>
        <xsl:value-of select="regex-group(1)"/>
        <xsl:value-of select="2 * number(regex-group(2))"/>
        <xsl:value-of select="regex-group(3)"/>
    </xsl:matching-substring>

    <xsl:non-matching-substring>
        <xsl:value-of select="."/>
    </xsl:non-matching-substring>

    </xsl:analyze-string>
</xsl:template>

请注意,这不会在字符串的开头或结尾捕获个位数.要包含这些,您必须使用:

Note that this does not capture single-digit numbers at the beginning or at the end of the string. To include these, you would have to use:

<xsl:analyze-string select="." regex="(^|\D)(\d)(\D|$)">

这篇关于XSLT - 正则表达式从 text() 节点中仅选择一位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆