XSL 1.0 Count与“following:[element]”不同,并排除某些元素

时间:2016-03-07 10:25:39

标签: xslt count distinct grouping

所以我一直在努力计算不同的元素。我有这些数据。代码已准备好进行复制粘贴。

<CustInvoiceTable class="entity">
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000088</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000088</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000091</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000091</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000098</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000098</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000086</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000086</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000062</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>2</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000062</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>2</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000111</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000111</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000089</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000089</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000092</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000092</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000101</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000101</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000102</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000102</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000083</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000083</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000067</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000067</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>1</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>

    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000125</ItemId>
        <McsCmBilProductItem class="entity">
            <CgiBundleLines>0</CgiBundleLines>
        </McsCmBilProductItem>
    </McsCmBilCalcInvoiceLine>

    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000069</ItemId>
    </McsCmBilCalcInvoiceLine>

    <!-- Excluding these 3 elements will result in the correct count -->
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000083</ItemId>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000092</ItemId>
    </McsCmBilCalcInvoiceLine>
    <McsCmBilCalcInvoiceLine class="entity">
        <ItemId>ITM-0000098</ItemId>
    </McsCmBilCalcInvoiceLine>

</CustInvoiceTable>

我想计算不同的ItemId值,其中CgiBundleLines = 1.结果必须是11.我的XSL:

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

    <xsl:key name="Lines-by-ItemId" match="CustInvoiceTable/McsCmBilCalcInvoiceLine" use="CustInvoiceTable/McsCmBilCalcInvoiceLine/ItemId" />

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">

        <CountOne><xsl:value-of select="count(CustInvoiceTable/McsCmBilCalcInvoiceLine[not(ItemId = following::ItemId) and McsCmBilProductItem/CgiBundleLines = 1])"/></CountOne>
        <CountOne><xsl:value-of select="count(CustInvoiceTable/McsCmBilCalcInvoiceLine[not(ItemId = following::ItemId) and McsCmBilProductItem/CgiBundleLines = 1 and boolean(McsCmBilProductItem) = 1])"/></CountOne>

    </xsl:template>

</xsl:stylesheet>

当我运行这个时,我得到8的计数。看起来最后3个McsCmBilCalcInvoiceLine元素导致ItemId不被计算。将这3个返回则返回count = 11,这就是我想要的。那么如何排除最后3个元素或使我的XSL代码不计算它们。

谢谢。

1 个答案:

答案 0 :(得分:1)

如果您使用单独的谓词/CustInvoiceTable/McsCmBilCalcInvoiceLine[McsCmBilProductItem/CgiBundleLines = 1][not(ItemId = preceding-sibling::McsCmBilCalcInvoiceLine/ItemId)],那么您只能计算具有McsCmBilProductItem/CgiBundleLines = 1条件的元素。

所以

count(/CustInvoiceTable/McsCmBilCalcInvoiceLine[McsCmBilProductItem/CgiBundleLines = 1][not(ItemId = preceding-sibling::McsCmBilCalcInvoiceLine/ItemId)])

给出了11。