首页 > 解决方案 > 使用字符串函数编辑数学公式

问题描述

输入xml:

<sheet-data>
  <variable name='A' formula='(varOne*1000/varTwo-varThree)/varFour'/>
  <variable name='B' formula='(-varOne)/varTwo'/>
  <variable name='C' formula='1000*varOne/(varTwo+varThree)'/>
</sheet-data>

我需要显示每个公式的值,但在公式中的每个“var”之后添加“.value”。使输出 xml 看起来像这样:

(varOne.value*1000/varTwo.value-varThree.value)/varFour.value
(-varOne.value)/varTwo.value
1000*varOne.value/(varTwo.value+varThree.value)

以下代码是 xslt 代码。我明白为什么这不起作用,但我不知道如何解决它......在旁注中,当前 xslt 代码的输出中没有分页符。有人对实现这一目标的最简单方法有建议吗?输出方法是="text"。

<xsl:template match='sheet-data'>
  <xsl:for-each select='./*'>
    <xsl:value-of select='replace("@formula", "[A-z]+", ".value")'/>
  </xsl:for-each>
</xsl:template>

标签: regexxslt

解决方案


怎么样:

<xsl:template match="/sheet-data">
    <xsl:for-each select="variable">
        <xsl:value-of select="replace(@formula, '(var[A-z]+)([^A-z]|$)', '$1.value$2')"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

这假设您的所有变量都以 string 开头"var"。如果这个假设不正确,您可以使用:

<xsl:value-of select="replace(@formula, '([A-z]+)', '$1.value')"/>

反而。但是,这假定任何连续字母字符的子字符串都是“var”。


推荐阅读