Question

Given the following document structure...

<orders>
   <order date="June-15-2008" item="F101" qty="1" customer="C28901" />
   <order date="June-16-2008" item="F114" qty="1" customer="C28902" />
   <order date="June-16-2008" item="F135" qty="1" customer="C28906" />
   <order date="June-17-2008" item="F108" qty="1" customer="C28908" />
   <order date="June-15-2008" item="F108" qty="1" customer="C28910" />
   <order date="June-16-2008" item="F118" qty="4" customer="C28914" />
   <order date="June-15-2008" item="F105" qty="5" customer="C28915" />
   <order date="June-17-2008" item="F120" qty="2" customer="C28919" />
   <order date="June-16-2008" item="F131" qty="1" customer="C28920" />
</orders>

I am trying to use muenchian grouping to select the first ordr element for each unique date and have tried each of the following to no avail...

<xsl:for-each select="//order/@date[generate-id()=generate-id(key(date, @date)[1])]" >
    ... do something here ...
</xsl:for-each>

<xsl:for-each select="/orders/order/@date[generate-id()=generate-id(key(date, @date)[1])]" >
    ... do something here ...
</xsl:for-each>

<xsl:for-each select="/orders/order[generate-id()=generate-id(key(date, @date)[1])]" >
    ... do something here ...
</xsl:for-each>

I am having a very difficult time understanding muenchian grouping through and through.

Was it helpful?

Solution

try this

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

    <!-- get keys of order using the date attribute -->
    <xsl:key name="dates" match="order" use="@date" />

    <xsl:template match="orders">
        <xsl:for-each select="order[generate-id()=generate-id(key('dates', @date)[1])]">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

given your source input, I have the following output

<order date="June-15-2008" item="F101" qty="1" customer="C28901"/>
<order date="June-16-2008" item="F114" qty="1" customer="C28902"/>
<order date="June-17-2008" item="F108" qty="1" customer="C28908"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top