Search code examples
xmlxsltxslt-2.0xslkey

XSLT xsl:key - exclude input from report


`

<Fruits>
    <Fruit>
        <Name>Mango</Name>
        <Price>20</Price>
        <Vendor>Vendor1</Vendor>
        <State>AK</State>
        <Status>Delivered</Status>
        <ProductCode>123</ProductCode>
    </Fruit>
    <Fruit>
        <Name>Apple</Name>
        <Price>34</Price>
        <Vendor>Vendor2</Vendor>
        <State>AS</State>
        <Status>Delivered</Status>
        <ProductCode>111</ProductCode>
    </Fruit>
    <Fruit>
        <Name>Mango</Name>
        <Price>20</Price>
        <Vendor>Vendor3</Vendor>
        <State>FL</State>
        <Status>Delivered</Status>
        <ProductCode>123</ProductCode>
    </Fruit>
    <Fruit>
        <Name>Papaya</Name>
        <Price>5</Price>
        <Vendor>Vendor4</Vendor>
        <State>CA</State>
        <Status>Sold</Status>
        <ProductCode>222</ProductCode>
</Fruit>
</Fruits>

`

I don't wan to include Mango to report because the status is Delivered to another state with the same ProductCode, Price and Status.

Expected output:

ProductCode Fruit Price 111 Apple 34 222 Papaya 5


Solution

  • Something like this:

    <xsl:for-each-group select="Fruit" 
      group-by="string-join((ProductGroup, Price, Status), '~')">
      <xsl:apply-templates select="current-group()[last()=1]"/>
    </xsl:for-each-group>
    

    The effect of the predicate [last()=1] is to process the group only if its size is 1.