[英]XSLT - changes number exist within text() node
我正在使用XSLT进行xml到xml的转换,并且具有如下的XML代码。
<section>
<para>height 10cm, width 15cm</para>
<para>height 20cm, width 15cm</para>
<para>height 10cm, width 22cm</para>
</section>
在这里,我需要将输出中的高度和宽度值加倍。 因此,转换后的xml是
<section>
<para>height 20cm, width 30cm</para>
<para>height 40cm, width 30cm</para>
<para>height 20cm, width 44cm</para>
</section>
我考虑过使用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>
但它不能按预期工作。
谁能建议我一种方法,如何将para元素中存在的数字加倍?
我远不是正则表达式专家,但这似乎对我有用:
<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.