By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

How 2 remove duplicate element based on combination of multiple attributes using xslt

P: 2
This is how my Input XML looks like:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3. <Account  InceptionDate="03/01/2005"  BusinessWorkGroup="US" BusinessUnit="BOS" AccountId="XYZ4321" ISOCurrency="USD" DomicileCountry="USA">
  4. <AssignContact ContactId="123" Type="Administrative Assistant"/>
  5. <AssignContact ContactId="123" Type="Portfolio Manager"/>
  6. <AssignContact ContactId="123" Type="Portfolio Manager"/>
  7. </Account>  
  8. <Account  InceptionDate="03/01/2006"  BusinessWorkGroup="US" BusinessUnit="BOS" AccountId="ABC1231" ISOCurrency="USD" DomicileCountry="USA">
  9. <AssignContact ContactId="543" Type="Administrative Assistant"/>
  10. <AssignContact ContactId="543" Type="Portfolio Manager"/>
  11. <AssignContact ContactId="211" Type="Portfolio Manager"/>
  12. </Account>  
  13. <Account  InceptionDate="03/01/2010"  BusinessWorkGroup="US" BusinessUnit="BOS" AccountId="DUM111" ISOCurrency="USD" DomicileCountry="USA">
  14. <AssignContact ContactId="987" Type="Administrative Assistant"/>
  15. <AssignContact ContactId="987" Type="Administrative Assistant"/>
  16. <AssignContact ContactId="211" Type="Portfolio Manager"/>
  17. </Account> 
  18. </root>
My output should look like (Duplicate [combination of ContactId & Type ] should be removed from final xml ):

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3. <Account  InceptionDate="03/01/2005"  BusinessWorkGroup="US" BusinessUnit="BOS" AccountId="XYZ4321" ISOCurrency="USD" DomicileCountry="USA">
  4. <AssignContact ContactId="123" Type="Administrative Assistant"/>
  5. <AssignContact ContactId="123" Type="Portfolio Manager"/>
  6. </Account>  
  7. <Account  InceptionDate="03/01/2006"  BusinessWorkGroup="US" BusinessUnit="BOS" AccountId="ABC1231" ISOCurrency="USD" DomicileCountry="USA">
  8. <AssignContact ContactId="543" Type="Administrative Assistant"/>
  9. <AssignContact ContactId="543" Type="Portfolio Manager"/>
  10. <AssignContact ContactId="211" Type="Portfolio Manager"/>
  11. </Account>
  12. <Account  InceptionDate="03/01/2010"  BusinessWorkGroup="US" BusinessUnit="BOS" AccountId="DUM111" ISOCurrency="USD" DomicileCountry="USA">
  13. <AssignContact ContactId="987" Type="Administrative Assistant"/>
  14. <AssignContact ContactId="211" Type="Portfolio Manager"/>
  15. </Account> 
  16. </root>
Feb 13 '15 #1

✓ answered by deepak Khatri

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  2.     <xsl:output method="xml" indent="yes" version="1.0"/>
  3.     <xsl:strip-space elements="*"/>
  4.  
  5.     <xsl:key name="Contact" match="AssignContact" use="concat(../@AccountId, '|', @ContactId, '|', @Type)" />
  6.  
  7.     <xsl:template match="@*|node()">
  8.         <xsl:copy>
  9.             <xsl:apply-templates select="@*|node()"/>
  10.             </xsl:copy>
  11.     </xsl:template>
  12.  
  13.     <xsl:template match="AssignContact[generate-id() != generate-id(key('Contact', concat(../@AccountId, '|', @ContactId, '|', @Type))[1])]" />
  14. </xsl:stylesheet>

Share this Question
Share on Google+
1 Reply


P: 2
Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  2.     <xsl:output method="xml" indent="yes" version="1.0"/>
  3.     <xsl:strip-space elements="*"/>
  4.  
  5.     <xsl:key name="Contact" match="AssignContact" use="concat(../@AccountId, '|', @ContactId, '|', @Type)" />
  6.  
  7.     <xsl:template match="@*|node()">
  8.         <xsl:copy>
  9.             <xsl:apply-templates select="@*|node()"/>
  10.             </xsl:copy>
  11.     </xsl:template>
  12.  
  13.     <xsl:template match="AssignContact[generate-id() != generate-id(key('Contact', concat(../@AccountId, '|', @ContactId, '|', @Type))[1])]" />
  14. </xsl:stylesheet>
Feb 17 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.