I've xml like this,
<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>
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,
current output,
<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:
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:
There are several ways to solve the problem. One way is to require that the single digit is followed by "cm" (if that's always the case in your input XML, we don't know yet).
XSLT Stylesheet
XML Output
Alternatively, you could e.g. require that the single digit is followed by something that is not a digit:
If that always applies to your data, because it does not cover cases where there is a single digit at the very end of a string.
To account for all possible cases, use
which is essentially the same as michael.hor257k has suggested (before I did!).