XSL 中每组值的最大总和

发布于 2024-09-12 08:22:28 字数 698 浏览 2 评论 0原文

对于这个问题可能有一个非常简单的解决方案。我可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-09-19 08:22:28

我。一个好的 XSLT 1.0 解决方案(简洁、高效且易于理解):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
   <xsl:for-each select="group">
     <xsl:sort select="sum(val)" data-type="number"
      order="descending"/>

     <xsl:if test="position()=1">
       <xsl:value-of select="sum(val)"/>
     </xsl:if>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时

<t>
    <group>
        <val>1</val>
        <val>3</val>
        <val>1</val>
    </group>
    <group>
        <val>3</val>
        <val>2</val>
        <val>2</val>
    </group>
</t>

生成所需的正确结果

7

要获取所需的变量定义,只需将上述代码中的 指令放入变量主体即可。

二.一种更好的 XSLT 2.0(实际上是 XPath 2.0 单行)解决方案:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
   <xsl:sequence select="max(group/sum(val))"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一个 XML 文档时,会产生相同的正确答案

7

并且想要的变量定义很简单:

  <xsl:variable name="vHighestSum" 
       select="max(group/sum(val))"/>

最后,可以在 XQuery 中使用相同的 Xpath 表达式来定义所需的变量:

let $vHighestSum := max(/*/group/sum(val))

I. A good XSLT 1.0 solution (brief, efficient and understandable):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
   <xsl:for-each select="group">
     <xsl:sort select="sum(val)" data-type="number"
      order="descending"/>

     <xsl:if test="position()=1">
       <xsl:value-of select="sum(val)"/>
     </xsl:if>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>
    <group>
        <val>1</val>
        <val>3</val>
        <val>1</val>
    </group>
    <group>
        <val>3</val>
        <val>2</val>
        <val>2</val>
    </group>
</t>

the wanted, correct result is produced:

7

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:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
   <xsl:sequence select="max(group/sum(val))"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document, the same correct answer is produced:

7

And the wanted variable definition is simply:

  <xsl:variable name="vHighestSum" 
       select="max(group/sum(val))"/>

Finally, the same Xpath expression can be used in XQuery to define the required variable:

let $vHighestSum := max(/*/group/sum(val))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文