简体   繁体   English

XSLT-更改数量存在于text()节点中

[英]XSLT - changes number exist within text() node

I'm doing xml to xml transformation using XSLT and I have a XML code like follows. 我正在使用XSLT进行xml到xml的转换,并且具有如下的XML代码。

<section>
   <para>height 10cm, width 15cm</para>
   <para>height 20cm, width 15cm</para>
   <para>height 10cm, width 22cm</para>
</section>

here I need to double the height and width value in the output. 在这里,我需要将输出中的高度和宽度值加倍。 So transformed xml would be, 因此,转换后的xml是

<section>
   <para>height 20cm, width 30cm</para>
   <para>height 40cm, width 30cm</para>
   <para>height 20cm, width 44cm</para>
</section>

I thought about use XSLT regex to solve this matter and wrote following template, 我考虑过使用XSLT正则表达式解决此问题,并编写了以下模板,

<xsl:template match="para/text()">
        <xsl:variable name="elValue" select="."/>

        <xsl:analyze-string select="$elValue" regex="(\d{{5}}(\-\d{{4}})?)\s*">

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

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

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

but it does not work as expected. 但它不能按预期工作。

can anyone suggest me a method how can I doubled that numbers exist within para elements? 谁能建议我一种方法,如何将para元素中存在的数字加倍?

I am far from being a regex whiz, but this seems to be working for me: 我远不是正则表达式专家,但这似乎对我有用:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM