Request Payload:
<GetDocumentsResponse>
<tns:DocumentDetails>
<tns:DocumentId>217</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>12312173</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>218</tns:DocumentId>
<tns:DocumentSource>W</tns:DocumentSource>
<tns:DocumentName>Test document 10</tns:DocumentName>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>217</tns:DocumentId>
<tns:DocumentSource>W</tns:DocumentSource>
<tns:DocumentName>Test document 13</tns:DocumentName>
</tns:DocumentDetails>
<GetDocumentsResponse>
<tns:DocumentDetails>
<tns:DocumentId>217</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>12312173</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>218</tns:DocumentId>
<tns:DocumentSource>W</tns:DocumentSource>
<tns:DocumentName>Test document 10</tns:DocumentName>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>217</tns:DocumentId>
<tns:DocumentSource>W</tns:DocumentSource>
<tns:DocumentName>Test document 13</tns:DocumentName>
</tns:DocumentDetails>
</GetDocumentsResponse>
Expected Response:
Eliminated duplicate records using one of the child nodes (DocumentId here). Eliminate duplicated DocumentId="217" record from the response just like below.
<GetDocumentsResponse>
<tns:DocumentDetails>
<tns:DocumentId>217</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>12312173</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>218</tns:DocumentId>
<tns:DocumentSource>W</tns:DocumentSource>
<tns:DocumentName>Test document 10</tns:DocumentName>
</tns:DocumentDetails>
</GetDocumentsResponse>
<tns:DocumentDetails>
<tns:DocumentId>217</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>12312173</tns:DocumentId>
<tns:DocumentSource>WCC</tns:DocumentSource>
</tns:DocumentDetails>
<tns:DocumentDetails>
<tns:DocumentId>218</tns:DocumentId>
<tns:DocumentSource>W</tns:DocumentSource>
<tns:DocumentName>Test document 10</tns:DocumentName>
</tns:DocumentDetails>
</GetDocumentsResponse>
Solution:
XSLT 1.0:
Use the below xslt template with version 1.0.
Use the below xslt template with version 1.0.
<xsl:stylesheet version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="DocumentDetailsById" match="/ns0:GetDocumentsResponse/ns0:DocumentDetails" use="./ns0:DocumentId"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/ns0:GetDocumentsResponse/ns0:DocumentDetails[not(generate-id() = generate-id(key('DocumentDetailsById', ./ns0:DocumentId[1])))]"/>
</xsl:stylesheet>
XSLT 2.0:
<xsl:stylesheet version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<XML>
<xsl:for-each-group select="/ns0:GetDocumentsResponse/ns0:DocumentDetails" group-by="./ns0:DocumentId">
<xsl:sequence select="."/>
</xsl:for-each-group>
</XML>
</xsl:template>
</xsl:stylesheet>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<XML>
<xsl:for-each-group select="/ns0:GetDocumentsResponse/ns0:DocumentDetails" group-by="./ns0:DocumentId">
<xsl:sequence select="."/>
</xsl:for-each-group>
</XML>
</xsl:template>
</xsl:stylesheet>