简体   繁体   中英

XSLT - changes number exist within text() node

I'm doing xml to xml transformation using XSLT and I have a XML code like follows.

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

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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