XSLT 1.0嵌套分组,总和&计数

时间:2014-02-28 05:09:05

标签: xml xslt xslt-grouping

在XSLT 1.0中,需要按地址分组,然后按部分和总计分组。目前,如果该部分未包括在第一个地址元素中,则省略它。对此非常陌生。

输出是 -

Total Units 7

name1address1
part1 Total Pallets : 1
part2 Total Pallets : 2

name2address2
part1 Total Pallets : 2
part2 Total Pallets : 1

应该 - (注意地址2中的第3部分)

Total Units 7

name1address1
part1 Total Pallets : 1
part2 Total Pallets : 2

name2address2
part1 Total Pallets : 2
part2 Total Pallets : 1
part3 Total Pallets : 1

XML是 -

<?xml version="1.0" encoding="UTF-8"?>
<dispatch>
    <delivery>
        <name>name2</name>
        <address>address2</address>
        <pallet>
            <part>part1</part>
        </pallet>
        <pallet>
            <part>part2</part>
        </pallet>
    </delivery>
    <delivery>
        <name>name1</name>
        <address>address1</address>
        <pallet>
            <part>part1</part>
        </pallet>
        <pallet>
            <part>part2</part>
        </pallet>
        <pallet>
            <part>part2</part>
        </pallet>
    </delivery>
    <delivery>
        <name>name2</name>
        <address>address2</address>
        <pallet>
            <part>part1</part>
        </pallet>
        <pallet>
            <part>part3</part>
        </pallet>
    </delivery>
</dispatch>

XSLT是 -

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

    <xsl:key name="delivery_detail" match="delivery" use="concat(name,address)"/>
    <xsl:key name="delivery_detail_part" match="pallet" use="concat(../name,../address,part)"/>


    <xsl:template match="dispatch">
        <xsl:variable name="total_units" select="count(delivery/pallet)"/>
        <value>Total Units </value>
        <xsl:value-of select="$total_units"/>
        <br/>

        <xsl:apply-templates
            select="delivery[generate-id(.)=generate-id(key('delivery_detail',concat(name,address))[1])]">
            <xsl:with-param name="total_units" select="$total_units"/>
            <xsl:sort select="name"/>
            <xsl:sort select="address"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="delivery">
        <br/>
        <xsl:value-of select="name"/>
        <xsl:value-of select="address"/>
        <br/>
        <xsl:apply-templates
            select="pallet[generate-id(.)=generate-id(key('delivery_detail_part',concat(../name,../address,part))[1])]"
        > </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="pallet">
        <xsl:value-of select="part"/>
        <xsl:variable name="total_pallets"
            select="count(key('delivery_detail_part', concat(../name,../address,part)))"/>
        <value> Total Pallets : <xsl:value-of select="$total_pallets"/></value>
        <br/>
    </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您只将托盘模板应用于名称/地址组中第一次投放的子项。尝试更改此内容:

<xsl:apply-templates
    select="pallet[generate-id(.)=generate-id(key('delivery_detail_part',concat(../name,../address,part))[1])]"> 
</xsl:apply-templates>

为:

<xsl:apply-templates
    select="key('delivery_detail', concat(name, address))/pallet[generate-id(.)=generate-id(key('delivery_detail_part',concat(../name,../address,part))[1])]">
</xsl:apply-templates>