XSLT 1.0 累加、求和、乘法
我有一组固定的节点:、
、
、< d>
。每个节点的值可以为 1 或 0。 此外,每个节点的权重分别为:1、2、3、4。不使用节点属性。 如何使用 XSLT 1.0 将每个节点的值乘以其权重求和?示例:
<a>0</a>
<b>1</b>
<c>0</c>
<d>1</d>
总和:6
<a>1</a>
<b>1</b>
<c>0</c>
<d>0</d>
总和:3
<a>0</a>
<b>1</b>
<c>1</c>
<d>1</d>
总和:9
I have a small fixed set of nodes: <a>
, <b>
, <c>
, <d>
. Each node can have a value of either 1 or 0.
Also, each node has a weight: 1, 2, 3, 4, respectively. Node attributes are not used.
How can I sum the value of each node multiplied by its weight using XSLT 1.0? Example:
<a>0</a>
<b>1</b>
<c>0</c>
<d>1</d>
Sum: 6
<a>1</a>
<b>1</b>
<c>0</c>
<d>0</d>
Sum: 3
<a>0</a>
<b>1</b>
<c>1</c>
<d>1</d>
Sum: 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此 XSLT 1.0 转换:
应用于提供的 XML 文档时:
3
分别产生所需的结果:
6
和
9
参考:
如果您想使用 XSLT 进行真正复杂的数学计算,请参阅:
http://fxsl.sourceforge.net/articles/xslCalculator/The%20FXSL%20Calculator.html
XPath 2.0 / XSLT 2.0 解决方案:
仅使用这个 XPath 2.0 一行:
This XSLT 1.0 transformation:
when applied to the provided XML documents:
,
and
produces the wanted results, respectively:
6
3
9
Reference:
If you want to get your hands dirty with really complicated math calculations using XSLT, see this:
http://fxsl.sourceforge.net/articles/xslCalculator/The%20FXSL%20Calculator.html
XPath 2.0 / XSLT 2.0 Solution:
Use just this XPath 2.0 one-liner:
不太优雅,但这就是我的想法。
当应用于以下格式正确的文档时:
结果为
6
结果为
3
结果为
9
Not really elegant, but that's what came to my mind.
When applied to the following well-formed documents:
Result is
6
Result is
3
Result is
9