regex - 使用字符串函数编辑数学公式
问题描述
输入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>
解决方案
怎么样:
<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> </xsl:text>
</xsl:for-each>
</xsl:template>
这假设您的所有变量都以 string 开头"var"
。如果这个假设不正确,您可以使用:
<xsl:value-of select="replace(@formula, '([A-z]+)', '$1.value')"/>
反而。但是,这假定任何连续字母字符的子字符串都是“var”。
推荐阅读
- javascript - 如何从与 Javascript Traverson HAL 的链接中获取 JSON 对象
- google-cloud-vertex-ai - 如何删除在 Google Vertex AI 中创建的元存储
- java - @PostMapping 在 Postman 中工作但不在浏览器中
- r - 单击任何按钮时采取行动 Shiny R
- mcedit - 如何在 BASH 环境的文本编辑器上复制和粘贴代码字符串
- go - 执行命令行二进制并连续读取标准输出
- node.js - 使用带有 Next.js 的 Typescript 找不到具有相应类型声明的模块
- javascript - 当我将鼠标悬停在链接的部分周围时,我想更改超链接的类别
- graphql - 来自 useStaticQuery 的 Gatsby 结果未定义
- angular - Ngrx 选择返回未定义