XSLT 1.0组和总和项-获取每个级别的房间项列表

时间:2018-06-26 04:24:32

标签: xml xslt sum grouping xslt-1.0

每个级别都有房间列表。
每个房间都有一个事件列表。
一个事件包含对房间中物品及其数量的引用。
一篇文章有​​代码和费用。

如何获取每个级别的文章列表(按代码分组以汇总数量/成本)?

XML:

<planning>
<articles>
    <article id="1" code="C12" name="chair" cost="20"/>
    <article id="2" code="T1" name="table" cost="31"/>
    <article id="3" code="L10" name="lamp" cost="10"/>
    <article id="4" code="TA2" name="tap" cost="5"/>
    <article id="5" code="B20" name="bed" cost="12"/>
</articles>
<rooms>
    <room id="1" name="A1">
        <occurrence-list>
            <occurrence id="1" article-ref="1" qty="3"/>
            <occurrence id="2" article-ref="4" qty="2"/>
            <occurrence id="3" article-ref="5" qty="4"/>
        </occurrence-list>
    </room>
    <room id="2" name="A2">
        <occurrence-list>
            <occurrence id="4" article-ref="1" qty="2"/>
            <occurrence id="5" article-ref="2" qty="3"/>
        </occurrence-list>
    </room>
    <room id="3" name="B1">
        <occurrence-list>
            <occurrence id="6" article-ref="1" qty="1"/>
            <occurrence id="7" article-ref="3" qty="2"/>
            <occurrence id="8" article-ref="4" qty="4"/>
        </occurrence-list>
    </room>
    <room id="4" name="B2">
        <occurrence-list>
            <occurrence id="9" article-ref="2" qty="2"/>
            <occurrence id="10" article-ref="5" qty="5"/>
        </occurrence-list>
    </room>
    <room id="5" name="C1">
        <occurrence-list>
            <occurrence id="10" article-ref="1" qty="2"/>
            <occurrence id="11" article-ref="3" qty="2"/>
            <occurrence id="12" article-ref="4" qty="1"/>
        </occurrence-list>
    </room>
</rooms>
<level id="1" name="Level 1">
    <level-room room-ref="1"/>
    <level-room room-ref="3"/>
    <level-room room-ref="5"/>
</level>
<level id="2" name="Level 2">
    <level-room room-ref="2"/>
    <level-room room-ref="4"/>
</level>

到目前为止,这是我的XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="utf-8" indent="no"/>
<xsl:key name="articles" match="article" use="@id"/>
<xsl:key name="room" match="room" use="@id"/>
<xsl:key name="room-occurrences" match="occurrence" use="../../@id"/>
<xsl:key name="level-rooms" match="level-room" use="../@id"/>
<xsl:template match="/">
    <html>
        <body>
            <h2>Items per level</h2>
            <table border="1">
                    <tr>
                        <td>Level</td>
                        <td>Article</td>
                        <td>Cost</td>
                        <td>Qty</td>
                        <td>Total</td>
                    </tr>
                <xsl:for-each select="/planning/level">
                    <tr>
                        <td><xsl:value-of select="@name"/></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                  <xsl:for-each select="key('room-occurrences', key('level-rooms', @id)/@room-ref)">
                    <xsl:sort select="key('articles', @article-ref)/@code" order="ascending"/>
                    <tr>  
                      <td></td>
                      <td><xsl:value-of select="key('articles', @article-ref)/@code"/></td>
                      <td><xsl:value-of select="key('articles', @article-ref)/@cost"/></td>
                      <td><xsl:value-of select="@qty"/></td>
                      <td><xsl:value-of select="@qty * key('articles', @article-ref)/@cost"/></td>
                    </tr>  
                   </xsl:for-each>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

这是我得到的结果HTML:

+---------+------+-----+-------+  
| Article | Cost | Qty | Total |  
+---------+------+-----+-------+  
| Level 1                      |  
+------------------------------+  
| B20     | 12   | 4   | 48    |  
+---------+------+-----+-------+  
| C12     | 20   | 3   | 60    |  
+---------+------+-----+-------+  
| C12     | 20   | 1   | 20    |  
+---------+------+-----+-------+  
| C12     | 20   | 2   | 40    |  
+---------+------+-----+-------+  
| L10     | 10   | 2   | 20    |  
+---------+------+-----+-------+  
| L10     | 10   | 2   | 20    |  
+---------+------+-----+-------+  
| TA2     | 5    | 2   | 10    |  
+---------+------+-----+-------+  
| TA2     | 5    | 4   | 20    |  
+---------+------+-----+-------+  
| TA2     | 5    | 1   | 5     |  
+---------+------+-----+-------+  
| Level 2                      |  
+------------------------------+  
| B20     | 12   | 5   | 60    |  
+---------+------+-----+-------+  
| C12     | 20   | 2   | 40    |  
+---------+------+-----+-------+  
| T1      | 31   | 3   | 93    |  
+---------+------+-----+-------+  
| T1      | 31   | 2   | 62    |  
+---------+------+-----+-------+  

但是我想按代码对文章进行分组,如下所示:

+---------+------+-----+-------+  
| Article | Cost | Qty | Total |  
+---------+------+-----+-------+  
| Level 1                      |  
+------------------------------+  
| B20     | 12   | 4   | 48    |
+---------+------+-----+-------+
| C12     | 20   | 6   | 120   |
+---------+------+-----+-------+
| L10     | 10   | 4   | 40    |
+---------+------+-----+-------+
| TA2     | 5    | 7   | 35    |
+---------+------+-----+-------+
| Level 2                      |
+------------------------------+
| B20     | 12   | 5   | 60    |
+---------+------+-----+-------+
| C12     | 20   | 2   | 40    |
+---------+------+-----+-------+
| T1      | 31   | 5   | 155   |
+---------+------+-----+-------+

1 个答案:

答案 0 :(得分:1)

由于您拥有独特的article元素,我认为一种方法是简单地使用已设置的键来选择它们(尽管在可能的情况下,我仅通过简单地使用子元素就将其缩短了一点),然后进行处理引用的article,回顾数量之和:

<xsl:key name="articles" match="article" use="@id"/>
<xsl:key name="room" match="room" use="@id"/>

<xsl:template match="/">
    <html>
        <body>
            <h2>Items per level</h2>
            <table border="1">
                    <tr>
                        <td>Level</td>
                        <td>Article</td>
                        <td>Cost</td>
                        <td>Qty</td>
                        <td>Total</td>
                    </tr>
                <xsl:for-each select="/planning/level">
                    <tr>
                        <td><xsl:value-of select="@name"/></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>

                  <xsl:variable name="level-occs" select="key('room', level-room/@room-ref)/occurrence-list/occurrence"/>
                  <xsl:for-each select="key('articles', $level-occs/@article-ref)">
                    <xsl:sort select="@code" order="ascending"/>
                    <xsl:variable name="qty" select="sum($level-occs[@article-ref = current()/@id]/@qty)"/>
                    <tr>  
                      <td></td>
                      <td><xsl:value-of select="@code"/></td>
                      <td><xsl:value-of select="@cost"/></td>
                      <td><xsl:value-of select="$qty"/></td>
                      <td><xsl:value-of select="$qty * @cost"/></td>
                    </tr>  
                   </xsl:for-each>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

https://xsltfiddle.liberty-development.net/6qVRKwc/2