How to remove the duplicate node values index based using xslt 1.0

I tried various ways to get rid of the duplicate node values but couldn’t achieve the desired result. Below is the input XML.

<beforechange>
    <area>
      <stud-no>244</stud-no>
      <stud-id>l13</stud-id>          
      <stud-class-buckets>
         <stud-type>351</stud-type>
         <stud-agent>19351</stud-agent>
         <stud-in-co>60112</stud-in-co>      
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>353</stud-type>
         <stud-agent>19353</stud-agent>
         <stud-ins-co>60122</stud-ins-co>        
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>352</stud-type>
         <stud-agent>19133</stud-agent>
         <stud-ins-co>60122</stud-ins-co>        
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>355</stud-type>
         <stud-agent>19355</stud-agent>
         <stud-ins-co>60122</stud-ins-co>        
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>[{low}]</stud-type>
         <stud-agent>60114</stud-agent>
         <stud-ins-co>19133</stud-ins-co>        
      </stud-class-buckets>   
    </area>
</beforechange>
<afterchange>
    <area>
      <stud-no>244</stud-no>
      <stud-id>l13</stud-id>         
      <stud-class-buckets>
         <stud-type>351</stud-type>
         <stud-agent>19351</stud-agent>
         <stud-in-co>60112</stud-in-co>                                 
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>353</stud-type>
         <stud-agent>19353</stud-agent>
         <stud-ins-co>60122</stud-ins-co>                                 
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>355</stud-type>
         <stud-agent>19355</stud-agent>
         <stud-ins-co>60122</stud-ins-co>                                 
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>[{low}]</stud-type>
         <stud-agent>19000</stud-agent>
         <stud-ins-co>60122</stud-ins-co>                                 
      </stud-class-buckets>
      <stud-class-buckets>
         <stud-type>[{low}]</stud-type>
         <stud-agent>19000</stud-agent>
         <stud-ins-co>60122</stud-ins-co>                                 
      </stud-class-buckets>                            
    </area>                        
</afterchange>

XSLT used for transformation –

<xsl:template match="/">
    <MaintenanceMessage>            
        <BeforeImage>
            <MaintenanceList>
                <xsl:apply-templates select="//beforechange/area/stud-class-buckets">
                    <xsl:with-param name="placement">beforechange</xsl:with-param>
                </xsl:apply-templates>
            </MaintenanceList>              
        </BeforeImage>
        <AfterImage>
            <MaintenanceList>
                <xsl:apply-templates select="//beforechange/area/stud-class-buckets">
                    <xsl:with-param name="placement">afterchange</xsl:with-param>
                </xsl:apply-templates>
            </MaintenanceList>              
        </AfterImage>
    </MaintenanceMessage>
</xsl:template>

<xsl:template match="//beforechange/area/stud-class-buckets">
        <xsl:param name="placement"/>
        <xsl:variable name="index" select="position()"/>
        
        <xsl:if test="//beforechange/area/stud-class-buckets[$index] != //afterchange/area/stud-class-buckets[$index]">         
            <Maintenance>               
                <xsl:attribute name="index"><xsl:value-of select="$index"/></xsl:attribute>             
                <xsl:variable name="TypeCode">
                    <xsl:call-template name="FieldValue">
                        <xsl:with-param name="value" select="dyn:evaluate(concat('//MESSAGE/', $placement , '/area/stud-class-buckets[$index]/stud-type'))"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="$TypeCode = 'null'">
                        <egm:TypeCode xsi:nil="true"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <egm:TypeCode>
                            <xsl:value-of select="$TypeCode"/>
                        </egm:TypeCode>
                    </xsl:otherwise>
                </xsl:choose>               
                <xsl:variable name="AgentCode">
                    <xsl:call-template name="FieldValue">
                        <xsl:with-param name="value" select="dyn:evaluate(concat('//MESSAGE/', $placement , '/area/stud-class-buckets[$index]/stud-agent'))"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="$AgentCode = 'null'">
                        <egm:AgentCode xsi:nil="true"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <egm:AgentCode>
                            <xsl:value-of select="$AgentCode"/>
                        </egm:AgentCode>
                    </xsl:otherwise>
                </xsl:choose>               
                <xsl:variable name="InsCoCode">
                    <xsl:call-template name="FieldValue">
                        <xsl:with-param name="value" select="dyn:evaluate(concat('//MESSAGE/', $placement , '/area/stud-class-buckets[$index]/stud-ins-co'))"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="$InsCoCode = 'null'">
                        <egm:InsCoCode xsi:nil="true"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <egm:InsCoCode>
                            <xsl:value-of select="$InsCoCode"/>
                        </egm:InsCoCode>
                    </xsl:otherwise>
                </xsl:choose>
        </Maintenance>      
    </xsl:if>   
</xsl:template> 

Output got –

<MaintenanceMessage>
    <BeforeImage>
        <MaintenanceList>
            <HazardMaintenance index="3">
                <TypeCode>352</TypeCode>
                <AgentCode>19133</AgentCode>
                <InsCoCode>60122</InsCoCode>                
            </HazardMaintenance>
            <HazardMaintenance index="4">
                <TypeCode>355</TypeCode>
                <AgentCode>19355</AgentCode>
                <InsCoCode>60122</InsCoCode>                
            </HazardMaintenance>
            <HazardMaintenance index="5">
                <TypeCode xsi:nil="true"/>
                <AgentCode>60114</AgentCode>
                <InsCoCode>19133</InsCoCode>                
            </HazardMaintenance>
        </MaintenanceList>
    </BeforeImage>
    <AfterImage>
        <MaintenanceList>
            <HazardMaintenance index="3">
                <TypeCode>355</TypeCode>
                <AgentCode>19355</AgentCode>
                <InsCoCode>60122</InsCoCode>                
            </HazardMaintenance>
            <HazardMaintenance index="4">
                <TypeCode xsi:nil="true"/>
                <AgentCode>19000</AgentCode>
                <InsCoCode>60122</InsCoCode>                
            </HazardMaintenance>
            <HazardMaintenance index="5">
                <TypeCode xsi:nil="true"/>
                <AgentCode>19000</AgentCode>
                <InsCoCode>60122</InsCoCode>                
            </HazardMaintenance>
        </MaintenanceList>
    </AfterImage>
</MaintenanceMessage>

Expected result is

<MaintenanceMessage>
    <BeforeImage>
        <MaintenanceList>
            <Maintenance index="1">
                <TypeCode>352</TypeCode>
                <AgentCode>19133</AgentCode>
                <InsCoCode>60122</InsCoCode>                
            </Maintenance>          
        </MaintenanceList>
    </BeforeImage>
    <AfterImage>
        <MaintenanceList>
            <Maintenance index="1">
                <TypeCode xsi:nil="true"/>
                <AgentCode>19000</AgentCode>
                <InsCoCode>60122</InsCoCode>                
            </Maintenance>          
        </MaintenanceList>
    </AfterImage>
</MaintenanceMessage>

In beforeimage & afterimage the TypeCode of index-5 should remove from the output.
In beforeimage & afterimage the TypeCode of index-4 & index-3 respectively should remove from the output & index value should be re-organized.
Or is there any way to transform the xml data based on the TypeCode value of beforeimage & afterimage data and then indexing it.
Am not so competent in XSLT. Appreciate the help. Thank you

New contributor

Prabina Kumar Samal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật