BizTalk Mapping Transformation - XSLT -Remove Duplicate Nodes

sushil 41 Reputation points
2021-09-15T15:22:06.277+00:00

I need to remove duplicate records from source and map to destination.
The source and destination schemas are same.

Source File/Schema:
<ns0:ExternalEmployees xmlns:ns0="http://BizTalk_Server_Project2.Schema1">
<packages>
<package>
<AddOns>
<AddOn-Bundled Category="Legal" SubType="Independent" Type="Core" />
<AddOn-Bundled Category="Home" SubType="Independent" Type="Core" />
<AddOn-Bundled Category="Home" SubType="Independent" Type="Core" />
</AddOns>
</package>
</packages>
</ns0:ExternalEmployees>

Map XSLT :
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:ns0="http://BizTalk_Server_Project2.Schema2" xmlns:s0="http://BizTalk_Server_Project2.Schema1">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<xsl:apply-templates select="/s0:ExternalEmployees" />
</xsl:template>
<xsl:template match="/s0:ExternalEmployees">
<ns0:ExternalEmployees>
<packages>
<xsl:for-each select="packages">
<xsl:for-each select="package">
<package>
<AddOns>
<xsl:for-each select="AddOns/AddOn-Bundled">
<AddOn-Bundled>
<xsl:if test="@Category">
<xsl:attribute name="Category">
<xsl:value-of select="@Category" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@SubType">
<xsl:attribute name="SubType">
<xsl:value-of select="@SubType" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@type ">
<xsl:attribute name="Type">
<xsl:value-of select="@type " />
</xsl:attribute>
</xsl:if>
</AddOn-Bundled>
</xsl:for-each>
</AddOns>
</package>
</xsl:for-each>
</xsl:for-each>
</packages>
</ns0:ExternalEmployees>
</xsl:template>
</xsl:stylesheet>

ExpectedOutput
<ns0:ExternalEmployees xmlns:ns0="http://BizTalk_Server_Project2.Schema1">
<packages>
<package>
<AddOns>
<AddOn-Bundled Category="Legal" SubType="Independent" Type="Core" />
<AddOn-Bundled Category="Home" SubType="Independent" Type="Core" />
</AddOns>
</package>
</packages>
</ns0:ExternalEmployees>

Microsoft BizTalk Server
Microsoft BizTalk Server
A family of Microsoft server products that support large-scale implementation management of enterprise application integration processes.
363 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yitzhak Khabinsky 25,841 Reputation points
    2021-09-15T18:38:54.703+00:00

    Hi @sushil ,

    By using XSLT 1.0 or 2.0

    Input XML

    <?xml version="1.0"?>  
    <ns0:ExternalEmployees xmlns:ns0="http://BizTalk_Server_Project2.Schema1">  
    	<packages>  
    		<package>  
    			<AddOns>  
    				<AddOn-Bundled Category="Legal" SubType="Independent" Type="Core"/>  
    				<AddOn-Bundled Category="Home" SubType="Independent" Type="Core"/>  
    				<AddOn-Bundled Category="Home" SubType="Independent" Type="Core"/>  
    			</AddOns>  
    		</package>  
    		<package>  
    			<AddOns>  
    				<AddOn-Bundled Category="Legal" SubType="Independent" Type="Core"/>  
    				<AddOn-Bundled Category="Home" SubType="Independent" Type="Core"/>  
    				<AddOn-Bundled Category="Home" SubType="Independent" Type="Core"/>  
    			</AddOns>  
    		</package>  
    	</packages>  
    </ns0:ExternalEmployees>  
    

    XSLT 2.0

    <?xml version="1.0"?>  
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>  
       <xsl:strip-space elements="*"/>  
    
     <xsl:template match="node()|@*">  
     <xsl:copy>  
     <xsl:apply-templates select="node()|@*"/>  
     </xsl:copy>  
     </xsl:template>  
      
     <xsl:template match="AddOns">  
     <xsl:copy>  
     <xsl:for-each-group select="AddOn-Bundled" group-by="concat(@Category, '|', @SubType, '|', @Type)">  
     <xsl:sequence select="."/>  
     </xsl:for-each-group>  
     </xsl:copy>  
     </xsl:template>  
    </xsl:stylesheet>  
    

    XSLT 1.0

    <?xml version="1.0"?>  
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
    	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>  
    	<xsl:strip-space elements="*"/>  
      
    	<xsl:template match="node()|@*">  
    		<xsl:copy>  
    			<xsl:apply-templates select="node()|@*"/>  
    		</xsl:copy>  
    	</xsl:template>  
      
    	<xsl:template match="AddOns">  
    		<xsl:copy>  
    			<xsl:for-each select="AddOn-Bundled[not(@Category=preceding-sibling::AddOn-Bundled/@Category)]">  
    				<xsl:sort select="@Category" order="descending" />  
    				<xsl:copy-of select="."/>  
    			</xsl:for-each>  
    		</xsl:copy>  
    	</xsl:template>  
    </xsl:stylesheet>  
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.