question

sushilauradkar avatar image
0 Votes"
sushilauradkar asked YitzhakKhabinsky-0887 commented

BizTalk Mapping Transformation - XSLT -Remove Duplicate Nodes

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>

biztalk-server
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@sushilauradkar,

What is your XSLT compliance level: 1.0, 2.0, or 3.0?

0 Votes 0 ·

While asking an XSLT question you need to provide a minimal reproducible example:
(1) Input XML.
(2) Your logic, and XSLT that tried to implement it.
(3) Desired output, based on the sample XML in #1 above
(4) XSLT processor and its compliance with the XSLT standards: 1.0, 2.0, or 3.0.

0 Votes 0 ·

1 Answer

YitzhakKhabinsky-0887 avatar image
1 Vote"
YitzhakKhabinsky-0887 answered YitzhakKhabinsky-0887 commented

Hi @sushilauradkar,

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>
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @YitzhakKhabinsky-0887

I am using XSLT 1.0.

Your solution works for one package node. I need to get it worked for multiple package nodes.

<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>

0 Votes 0 ·

@sushilauradkar,

I updated the answer for XSLT 1.0
Check it out.

0 Votes 0 ·