For the below input XML, I am able to group all the child <invoice>
elements under the appropriate <shippingBill>
element using muenchian grouping
method. However there is a requirement to include the count
of the number of <shippingBill>
elements and the <invoice>
elements in the final output XML. I am not sure how to go about it.
Input XML
<bank>
<shippingBills>
<shippingBill>
<shippingBillNo>5786885</shippingBillNo>
<shippingBillDate>10/02/2016</shippingBillDate>
<LEODate>11/02/2016</LEODate>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>183</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
</shippingBill>
<shippingBill>
<shippingBillNo>5786885</shippingBillNo>
<shippingBillDate>10/02/2016</shippingBillDate>
<LEODate>11/02/2016</LEODate>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>184</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
</shippingBill>
<shippingBill>
<shippingBillNo>3318135</shippingBillNo>
<shippingBillDate>01/10/2015</shippingBillDate>
<LEODate>01/10/2015</LEODate>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>172</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
</shippingBill>
<shippingBill>
<shippingBillNo>3318135</shippingBillNo>
<shippingBillDate>01/10/2015</shippingBillDate>
<LEODate>01/10/2015</LEODate>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>173</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
</shippingBill>
</shippingBills>
</bank>
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="key-bill" match="shippingBill" use="shippingBillNo"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="shippingBills">
<xsl:copy>
<xsl:apply-templates
select="shippingBill[generate-id() = generate-id(key('key-bill', shippingBillNo)[1])]"
mode="group" />
</xsl:copy>
</xsl:template>
<xsl:template match="shippingBill" mode="group">
<shippingBill>
<xsl:apply-templates select="*[not(self::invoice)]" />
<invoices>
<xsl:apply-templates select="key('key-bill', shippingBillNo)/invoice" />
</invoices>
</shippingBill>
</xsl:template>
</xsl:stylesheet>
Final Output XML Required
The <checkSum>
element has been added which has child elements that hold the respective counts.
<bank>
<checkSum>
<noOfInvoices>4</noOfInvoices>
<noOfShippingBills>2</noOfShippingBills>
</checkSum>
<shippingBills>
<shippingBill>
<shippingBillNo>5786885</shippingBillNo>
<shippingBillDate>10/02/2016</shippingBillDate>
<LEODate>11/02/2016</LEODate>
<invoices>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>183</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>184</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
</invoices>
</shippingBill>
<shippingBill>
<shippingBillNo>3318135</shippingBillNo>
<shippingBillDate>01/10/2015</shippingBillDate>
<LEODate>01/10/2015</LEODate>
<invoices>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>172</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>173</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
</invoices>
</shippingBill>
</shippingBills>
</bank>