sanjay
sanjay

Reputation: 1020

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?

Upvotes: 0

Views: 33

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

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>

Upvotes: 1

Related Questions