如何用两个节点分组并计算 XSLT 转换中的值

How to group by with two node and count the value in XSLT transformation

我对 xml 文件有要求:我必须按两个节点的值分组,并计算值 also.But 我是 XSLT 的新手,请帮助我。

输入Xml:

    <?xml version="1.0" encoding="utf-8"?>
    <Document>
        <TRU_SALESFORCEONHAND>
            <INVENTDIMID>#0000000150000BBB</INVENTDIMID>
            <INVENTLOCATIONID>W2472-Mezz</INVENTLOCATIONID>
            <INVENTSITEID>DC</INVENTSITEID>
            <ITEMID>000003</ITEMID>
            <ONHANDQTY>11.000000</ONHANDQTY>
        </TRU_SALESFORCEONHAND>
        <TRU_SALESFORCEONHAND>
            <INVENTDIMID>#0000000150000BBC</INVENTDIMID>
            <INVENTLOCATIONID>W2472-Mezz</INVENTLOCATIONID>
            <INVENTSITEID>DC</INVENTSITEID>
            <ITEMID>000003</ITEMID>
            <ONHANDQTY>3.000000</ONHANDQTY>
        </TRU_SALESFORCEONHAND>
        <TRU_SALESFORCEONHAND>
            <INVENTDIMID>#000000015000119A</INVENTDIMID>
            <INVENTLOCATIONID>W3501</INVENTLOCATIONID>
            <INVENTSITEID>DC</INVENTSITEID>
            <ITEMID>000004</ITEMID>
            <ONHANDQTY>23.000000</ONHANDQTY>
        </TRU_SALESFORCEONHAND>
        <TRU_SALESFORCEONHAND>
            <INVENTDIMID>#00000001500011AD</INVENTDIMID>
            <INVENTLOCATIONID>W3508</INVENTLOCATIONID>
            <INVENTSITEID>Site-ON</INVENTSITEID>
            <ITEMID>000003</ITEMID>
            <ONHANDQTY>1.000000</ONHANDQTY>
        </TRU_SALESFORCEONHAND>
        <TRU_SALESFORCEONHAND>
            <INVENTDIMID>#00000001500011AE</INVENTDIMID>
            <INVENTLOCATIONID>W3508</INVENTLOCATIONID>
            <INVENTSITEID>Site-ON</INVENTSITEID>
            <ITEMID>000004</ITEMID>
            <ONHANDQTY>8.000000</ONHANDQTY>
        </TRU_SALESFORCEONHAND>
        <TRU_SALESFORCEONHAND>
            <INVENTDIMID>#0000000150001A73</INVENTDIMID>
            <INVENTLOCATIONID>In_Transit</INVENTLOCATIONID>
            <INVENTSITEID>DC</INVENTSITEID>
            <ITEMID>000004</ITEMID>
            <ONHANDQTY>23.000000</ONHANDQTY>

</TRU_SALESFORCEONHAND>

    </Document>

输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<inventory
    xmlns="http://www.demandware.com/xml/impex/inventory/2007-05-31">
    <inventory-list>
        <header list-id="DC">
            <timestamp>2021-02-01T14:37:15.494Z</timestamp>
        </header>
        <records>
            <record product-id="000003">
                <allocation>14.000000</allocation>
                <allocation-timestamp>2021-02-01T14:37:15.494Z</allocation-timestamp>
            </record>
            <record product-id="000004">
                <allocation>23.000000</allocation>
                <allocation-timestamp>2021-02-01T14:37:15.494Z</allocation-timestamp>
            </record>
        </records>
    </inventory-list>
    <inventory-list>
        <header list-id="Site-ON">
            <timestamp>2021-02-01T14:37:15.494Z</timestamp>
        </header>
        <records>
            <record product-id="000003">
                <allocation>1.000000</allocation>
                <allocation-timestamp>2021-02-01T14:37:15.494Z</allocation-timestamp>
            </record>
            <record product-id="000004">
                <allocation>31.000000</allocation>
                <allocation-timestamp>2021-02-01T14:37:15.494Z</allocation-timestamp>
            </record>
        </records>
    </inventory-list>
</inventory>

带时间戳的是当前日期时间。我需要按 InventSiteId 对 header 条记录进行分组,所有 ITEMID 都属于该 InventSiteId。分配是该 ITEMID 的 ONHANDQTY 总和。

我尝试了很多次,但找不到方法。请帮助我,谢谢你们!

听起来好像你想嵌套两个 for-each-group 指令,如

<xsl:template match="/Document">
    <inventory>
      <xsl:for-each-group select="TRU_SALESFORCEONHAND" group-by="INVENTSITEID">
          <inventory-list>
              <header list-id="{current-grouping-key()}">
                  <timestamp>
                      <xsl:value-of select="current-dateTime()"/>
                  </timestamp>
                  <records>
                      <xsl:for-each-group select="current-group()" group-by="ITEMID">
                          <record product-id="{current-grouping-key()}">
                              <allocation>
                                  <xsl:value-of select="sum(current-group()/ONHANDQTY)"/>
                              </allocation>
                          </record>
                      </xsl:for-each-group>                      
                  </records>
              </header>
          </inventory-list>
      </xsl:for-each-group>
    </inventory>
</xsl:template>

不过,我没有得到你显示的金额,所以我可能误解了你的要求。