【问题标题】:sum by group XSLT "Munechian grouping"按组求和 XSLT“Munechian 分组”
【发布时间】:2011-12-20 23:25:11
【问题描述】:

似乎我暂时无法理解 Munechian 分组,所以我需要寻求帮助..

我试图总结所有我的类型 1 元素的价格。

我的 XML 文件如下所示。

<autoads>

    <ad>
        <type>1</type>
        <name>Honda</name>
        <model>XL 1000 V</model>
        <regyear>2001</regyear>
        <price>129900</price>
        <addate>20020115</addate>
        <volume>1000</volume>
        <category></category>
    </ad>
    <ad>
        <type>2</type>
        <name>Nissan</name>
        <model>Almera 1.4S</model>
        <regyear>1997</regyear>
        <price>119000</price>
        <addate>20020118</addate>
        <volume>0</volume>
        <category>5 dörrar</category>
    </ad>
....
</autoads>

所以我需要将所有类型 1 分组并总结它们的价格:

这是我迄今为止在我的 XSL 文件中所做的

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>
<body>

<h2>Fordon</h2>
<table border="1">
<tr bgcolor="#ccc">
<th>Namn</th>
<th>Modell</th>
<th>Beskrivning</th>
<th>Typ</th>

</tr>

<xsl:for-each select="//ad[type != '1']" >
<xsl:sort select="type" order="descending" />
<xsl:sort select="name"/>
<xsl:sort select="model"/>



<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="model" /></td>
<td><xsl:value-of select="adtext" /></td>
<td><xsl:value-of select="type" /></td> 
</tr>


</xsl:for-each>
</table>


<p>Summan av fordon är <xsl:value-of select="sum(//price)" /> SEK</p>
Antal bilar är <xsl:value-of select="count(/*/*/type[. = 2])"/><br /> 


</body>
</html>

</xsl:template>
</xsl:stylesheet>

我遇到的问题是我真的不知道在哪里放置这些行,所以它们不会干扰我的其余代码。

谢谢

【问题讨论】:

  • 我刚刚用这个唯一的字符串解决了我的问题 但我还是不明白分组:)
  • 不确定为什么需要对任何内容进行分组。

标签: xml xslt sum grouping


【解决方案1】:

对于 xslt,如果您想对类型 1 的所有价格求和

<xsl:value-of select="sum(//ad[type = '1']//price)" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2018-05-26
    • 2023-04-10
    • 2020-09-16
    • 2013-02-13
    • 2013-03-17
    • 2015-01-02
    相关资源
    最近更新 更多