2

First of i am sorry to say that "removing duplicate node not working all the ways as expected" even i referred multiple threads which are helped to some extend but still i have not reached to the solution what i expected.

To brief my case I want to remove XitemSup complex type element if my supplier and origin_country_id is same in following descendant.

Below is the xslt code

<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl xsd ns3"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns3="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier">
   <xsl:template match="/">
      <ns3:XItemSupDesc>
         <xsl:for-each select="//ns3:XItemSupDesc/ns3:XitemSup">             
            <xsl:if test="not(ns3:supplier=following::ns3:supplier) or not(ns3:origin_country_id=following::ns3:origin_country_id)">
                   <ns3:XitemSup>
                     <ns3:supplier>
                        <xsl:value-of select="./ns3:supplier"/>
                     </ns3:supplier>
                     <ns3:primary_supp_ind>
                        <xsl:value-of select="./ns3:primary_supp_ind"/>
                     </ns3:primary_supp_ind>
                     <ns3:origin_country_id>
                        <xsl:value-of select="./ns3:origin_country_id"/>
                     </ns3:origin_country_id>
                     <ns3:primary_country_ind>
                        <xsl:value-of select="./ns3:primary_country_ind"/>
                     </ns3:primary_country_ind>
                     <ns3:unit_cost>
                        <xsl:value-of select="./ns3:unit_cost"/>
                     </ns3:unit_cost>
                  </ns3:XitemSup>
               </xsl:if>
         </xsl:for-each>
      </ns3:XItemSupDesc>
   </xsl:template>
</xsl:stylesheet>

But if i apply on below xml it is not working

<XItemSupDesc xmlns:ns2="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier" xmlns="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier">
   <ns2:XitemSup>
      <ns2:supplier>101018</ns2:supplier>
      <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
      <ns2:origin_country_id>CA</ns2:origin_country_id>
      <ns2:primary_country_ind>N</ns2:primary_country_ind>
      <ns2:unit_cost>6</ns2:unit_cost>
   </ns2:XitemSup>
   <ns2:XitemSup>
      <ns2:supplier>102825</ns2:supplier>
      <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
      <ns2:origin_country_id>IN</ns2:origin_country_id>
      <ns2:primary_country_ind>N</ns2:primary_country_ind>
      <ns2:unit_cost>13</ns2:unit_cost>
   </ns2:XitemSup>
   <ns2:XitemSup>
      <ns2:supplier>102825</ns2:supplier>
      <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
      <ns2:origin_country_id>IN</ns2:origin_country_id>
      <ns2:primary_country_ind>N</ns2:primary_country_ind>
      <ns2:unit_cost>24</ns2:unit_cost>
   </ns2:XitemSup>
</XItemSupDesc>

I am hoping someone can let me know where I am going wrong with XSLT code.

1 Answer 1

1

I would recommend not using an xsl:for-each and just overriding the identity transform.

XML Input

<XItemSupDesc xmlns:ns2="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier" xmlns="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier">
    <ns2:XitemSup>
        <ns2:supplier>101018</ns2:supplier>
        <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
        <ns2:origin_country_id>CA</ns2:origin_country_id>
        <ns2:primary_country_ind>N</ns2:primary_country_ind>
        <ns2:unit_cost>6</ns2:unit_cost>
    </ns2:XitemSup>
    <ns2:XitemSup>
        <ns2:supplier>102825</ns2:supplier>
        <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
        <ns2:origin_country_id>IN</ns2:origin_country_id>
        <ns2:primary_country_ind>N</ns2:primary_country_ind>
        <ns2:unit_cost>13</ns2:unit_cost>
    </ns2:XitemSup>
    <ns2:XitemSup>
        <ns2:supplier>102825</ns2:supplier>
        <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
        <ns2:origin_country_id>IN</ns2:origin_country_id>
        <ns2:primary_country_ind>N</ns2:primary_country_ind>
        <ns2:unit_cost>24</ns2:unit_cost>
    </ns2:XitemSup>
</XItemSupDesc>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ns2="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ns2:XitemSup">
        <xsl:if test="not(following::ns2:XitemSup[ns2:supplier=current()/ns2:supplier and ns2:origin_country_id=current()/ns2:origin_country_id])">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>       
    </xsl:template>

</xsl:stylesheet>

XML Output

<XItemSupDesc xmlns="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier" xmlns:ns2="http://xmlns.oracle.com/TSS_to_RMS/RIBXItemProcess/Supplier">
   <ns2:XitemSup>
      <ns2:supplier>101018</ns2:supplier>
      <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
      <ns2:origin_country_id>CA</ns2:origin_country_id>
      <ns2:primary_country_ind>N</ns2:primary_country_ind>
      <ns2:unit_cost>6</ns2:unit_cost>
   </ns2:XitemSup>
   <ns2:XitemSup>
      <ns2:supplier>102825</ns2:supplier>
      <ns2:primary_supp_ind>N</ns2:primary_supp_ind>
      <ns2:origin_country_id>IN</ns2:origin_country_id>
      <ns2:primary_country_ind>N</ns2:primary_country_ind>
      <ns2:unit_cost>24</ns2:unit_cost>
   </ns2:XitemSup>
</XItemSupDesc>

Note: If you could use XSLT 2.0, you could get rid of the xsl:if and change the template to:

<xsl:template match="ns2:XitemSup[following::ns2:XitemSup[ns2:supplier=current()/ns2:supplier and ns2:origin_country_id=current()/ns2:origin_country_id]]"/>
Sign up to request clarification or add additional context in comments.

1 Comment

,Honestly your Awesome.. Your XSLT work like charm. Only thing i could get the result from xslt 2.0, but i used 1.0 which is giving desire result. First experience with stackoverflow is impressed. Thanks a ton again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.