I would like to be able to select one element from group of duplicates (done with Muenchian grouping) having certain sub-element. My XML looks like this:
<waybill>
<shipment>
<parcel>
<sscc>SSCC1</sscc>
<consignee>Receiver1</consignee>
<date>Date1</date>
<status>Status1</status>
</parcel>
<parcel>
<sscc>SSCC2</sscc>
<consignee>Receiver2</consignee>
<attention>Note2</attention>
</parcel>
<parcel>
<sscc>SSCC3</sscc>
<consignee>Receiver3</consignee>
</parcel>
<parcel>
<sscc>SSCC4</sscc>
<consignee>Receiver4</consignee>
</parcel>
<parcel>
<sscc>SSCC1</sscc>
<consignee>Receiver1</consignee>
<attention>Note1</attention>
<date>Date2</date>
<status>Status2</status>
</parcel>
<parcel>
<sscc>SSCC3</sscc>
<consignee>Receiver3</consignee>
<attention>Note3</attention>
</parcel>
</shipment>
</waybill>
and my XSLT looks like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="2.0">
<xsl:output method="text" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:key name="ean" match="parcel" use="sscc"/>
<xsl:template match="/">
<xsl:for-each select="/waybill/shipment/parcel[generate-id()=generate-id(key('ean',sscc))]">
<xsl:value-of select="current()/sscc"/>
<xsl:text>§</xsl:text>
<xsl:value-of select="current()/consignee"/>
<xsl:text>§</xsl:text>
<xsl:value-of select="current()/attention"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The output now looks like this:
SSCC1§Receiver1§
SSCC2§Receiver2§Note2
SSCC3§Receiver3§
SSCC4§Receiver4§
so my XSLT will now pick the first hit from the grouped elements and what I would like to have as a the result is:
SSCC1§Receiver1§Note1
SSCC2§Receiver2§Note2
SSCC3§Receiver3§Note3
SSCC4§Receiver4§
So the XSLT should find those duplicates from group with has the most data. Not first or last one, but the one with certain sub-element (or in my final case: sub-sub-element)
In this case not all of the element has the sub-element (here SSCC4) and those should be used with the data they have (should not be ignored).
What is the correct way to find the "best" element from the grouped similar elements?
Any help is highly appreciated :)