I am getting multiple RetrieveReplyDTO nodes and duplicate estimateNo fields. Requirement is , we have to find whether we are getting any duplicate estimateNo under RetrieveReplyDTO nodes and if we are getting any duplicate estimateNo , then check the creationDate and creationTime of that particular node "RetrieveReplyDTO " and get the latest estimate number .
Sample XML:
<?xml version="1.0" encoding="UTF-8" ?><ns0:retrieveResponse xmlns:ns0="some.url">
<ns0:out>
<ns0:replyElements>
<ns0:RetrieveReplyDTO>
<ns0:estimateNo>1234</ns0:estimateNo>
<ns0:estimateStatus>IN</ns0:estimateStatus>
<ns0:creationDate>20170220</ns0:creationDate>
<ns0:creationTime>111536</ns0:creationTime>
</ns0:RetrieveReplyDTO>
<ns0:RetrieveReplyDTO>
<ns0:estimateNo>1234</ns0:estimateNo>
<ns0:estimateStatus>IN</ns0:estimateStatus>
<ns0:creationDate>20170225</ns0:creationDate>
<ns0:creationTime>144500</ns0:creationTime>
</ns0:RetrieveReplyDTO>
<ns0:RetrieveReplyDTO>
<ns0:estimateNo>4567</ns0:estimateNo>
<ns0:estimateStatus>IN</ns0:estimateStatus>
<ns0:creationDate>20170230</ns0:creationDate>
<ns0:creationTime>144500</ns0:creationTime>
</ns0:RetrieveReplyDTO>
<ns0:RetrieveReplyDTO>
<ns0:estimateNo>4567</ns0:estimateNo>
<ns0:estimateStatus>IN</ns0:estimateStatus>
<ns0:creationDate>20170229</ns0:creationDate>
<ns0:creationTime>100045</ns0:creationTime>
</ns0:RetrieveReplyDTO>
</ns0:replyElements>
</ns0:out>
</ns0:retrieveResponse>
Below XSLT I have tried
<xsl:template match="/replyElements"> <xsl:for-each-group select="ns0:RetrieveReplyDTO" group-by="ns0:estimateNo">
<xsl:perform-sort select="current-group()">
<xsl:sort select="ns0:creationDate" order="descending" />
<xsl:sort select="ns0:creationTime" order="descending" />
</xsl:perform-sort>
</xsl:variable>
<xsl:copy-of select="." /></xsl:for-each-group></xsl:template>
I am getting below result:
<?xml version = '1.0' encoding = 'UTF-8'?><ns0:retrieveResponse ><ns0:out>
<ns0:replyElements>
<ns0:RetrieveReplyDTO>
<ns0:estimateNo>1234</ns0:estimateNo>
<ns0:estimateStatus>IN</ns0:estimateStatus>
<ns0:versionNo>001</ns0:versionNo>
<ns0:creationDate>20170225</ns0:creationDate>
<ns0:creationTime>144500</ns0:creationTime>
</ns0:RetrieveReplyDTO>
<ns0:RetrieveReplyDTO>
<ns0:estimateNo>4567</ns0:estimateNo>
<ns0:estimateStatus>IN</ns0:estimateStatus>
<ns0:creationDate>20170229</ns0:creationDate>
<ns0:creationTime>100045</ns0:creationTime>
</ns0:RetrieveReplyDTO>
</ns0:replyElements>
It is giving second search estimationno data , not doing any sort. please help
Inside of the for-each-group
you can use a for-each
with the xsl:sort
s and then you can output the first item in sort order, that is, change
<xsl:template match="/replyElements"> <xsl:for-each-group select="ns0:RetrieveReplyDTO" group-by="ns0:estimateNo">
<xsl:perform-sort select="current-group()">
<xsl:sort select="ns0:creationDate" order="descending" />
<xsl:sort select="ns0:creationTime" order="descending" />
</xsl:perform-sort>
</xsl:variable>
<xsl:copy-of select="." /></xsl:for-each-group></xsl:template>
to
<xsl:template match="replyElements">
<xsl:for-each-group select="ns0:RetrieveReplyDTO" group-by="ns0:estimateNo">
<xsl:for-each select="current-group()">
<xsl:sort select="ns0:creationDate" order="descending" />
<xsl:sort select="ns0:creationTime" order="descending" />
<xsl:if test="position() = 1">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
Note that I have also changed the match
pattern is the replyElements
is not the root element in your input snippet.