XSL 中每组值的最大总和
对于这个问题可能有一个非常简单的解决方案。我可以在 C#-LINQ 中轻松完成此操作。不幸的是,我对 XPath 和 XSL 不太有经验。
我有一个包含以下结构的输入 XML 文件:
<group>
<val>1</val>
<val>3</val>
<val>1</val>
</group>
<group>
<val>3</val>
<val>2</val>
<val>2</val>
</group>
现在,在我的 XSL 转换中,我想定义 1 个变量“highestsum”,其中包含“值”的最高总和。因此,对于该示例,它将返回 7,即第二组中所有值的总和。
经过一番搜索,这是我找到的最接近的解决方案:
http://w3schools.invisionzone。 com/index.php?showtopic=24265
但我有一种感觉,有比在模板中使用排序更好的方法来实现此结果。有接受者吗?
There is probably a very easy solution for this problem. I could easily do this in C#-LINQ. Unfortunately, I'm not so experienced with XPath and XSL.
I have an input XML file that contains the following structure:
<group>
<val>1</val>
<val>3</val>
<val>1</val>
</group>
<group>
<val>3</val>
<val>2</val>
<val>2</val>
</group>
Now in my XSL transform I want to define 1 variable "highestsum", which contains the highest sum of 'values'. So for the example, it would return 7, the sum of all values in the second group.
After some searching, this is the closest solution I found:
http://w3schools.invisionzone.com/index.php?showtopic=24265
But I have a feeling that there's a better way than using sorting in a template to achieve this result. Any takers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我。一个好的 XSLT 1.0 解决方案(简洁、高效且易于理解):
当此转换应用于以下 XML 文档时:
生成所需的正确结果:
要获取所需的变量定义,只需将上述代码中的
指令放入变量主体即可。二.一种更好的 XSLT 2.0(实际上是 XPath 2.0 单行)解决方案:
当此转换应用于同一个 XML 文档时,会产生相同的正确答案:
并且想要的变量定义很简单:
最后,可以在 XQuery 中使用相同的 Xpath 表达式来定义所需的变量:
I. A good XSLT 1.0 solution (brief, efficient and understandable):
when this transformation is applied on the following XML document:
the wanted, correct result is produced:
To get the desired variable definition, simply put the
<xsl:for-each>
instruvtion from the above code in the body of the variable.II. An even better XSLT 2.0 (and actually XPath 2.0 one-liner) solution:
when this transformation is applied on the same XML document, the same correct answer is produced:
And the wanted variable definition is simply:
Finally, the same Xpath expression can be used in XQuery to define the required variable: