xslt-2.0xslt-3.0

Remove duplicate ITEMs under one parent node


i am trying to remove duplicate BXYI/ITEM segments when its having same N11 and AR values under its respective parent node BXYI.

if multiple ITEM has same N11 and AR value then we can consider it as duplicate and we need to keep anyone occurrence, but if ITEM has Subnode exists then we need to consider that particular ITEM only, by removing other duplicate ITEMs.

XSLT i am using is exactly working, but for the input example i have given first item N11=003 and AR=170 is getting deleting as same set is available in the another BXYI/ITEM (last occurrence in example). my requirement is to look at the individual BXYI/ITEM segments. Please have a look.

Input sample

<?xml version="1.0" encoding="UTF-8"?>
<D02X001>
    <DOC BEGIN="1">
        <DC40 SEGMENT="1">
            <NAM>DC40</NAM>
        </DC40>
        <BXYH SEGMENT="1">
            <LDAT>date</LDAT>
            <UDAT>date1</UDAT>
            <BXYI SEGMENT="1">
                <TNR>123453</TNR>
                <ORT>1000</ORT>
                <ITEM SEGMENT="1">
                    <N11>003</N11>
                    <AR>170</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>003</N11>
                    <AR>17.40</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>999</N11>
                    <AR>17.40</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>999</N11>
                    <AR>17.40</AR>
                    <subnode SEGMENT="1">
                        <field1> 13</field1>
                    </subnode>
                </ITEM>
                <ITEMNEW SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                    <PQC>NU</PQC>
                    <QTY>3456</QTY>
                    <NUM/>
                    <ASCD/>
                </ITEMNEW>
            </BXYI>
            <BXYI SEGMENT="1">
                <TNR>123453</TNR>
                <ORT>1000</ORT>
                <ITEM SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>99</N11>
                    <AR>1.7</AR>
                    <subnode SEGMENT="1">
                        <field1> 13</field1>
                    </subnode>
                </ITEM>
                <ITEMNEW SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                    <PQC>NU</PQC>
                    <QTY>3456</QTY>
                    <NUM/>
                    <ASCD/>
                </ITEMNEW>
            </BXYI>
            <BXYI SEGMENT="1">
                <TNR>123453</TNR>
                <ORT>1000</ORT>
                <ITEM SEGMENT="1">
                    <N11>6789</N11>
                    <AR>035</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03424</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>003</N11>
                    <AR>170</AR>
                    <subnode SEGMENT="1">
                        <field1> 13</field1>
                    </subnode>
                </ITEM>
                <ITEMNEW SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                    <PQC>NU</PQC>
                    <QTY>3456</QTY>
                    <NUM/>
                    <ASCD/>
                </ITEMNEW>
            </BXYI>
        </BXYH>
    </DOC>
</D02X001>

output sample

<?xml version="1.0" encoding="UTF-8"?><D02X001>
    <DOC BEGIN="1">
        <DC40 SEGMENT="1">
            <NAM>DC40</NAM>
        </DC40>
        <BXYH SEGMENT="1">
            <LDAT>date</LDAT>
            <UDAT>date1</UDAT>
            <BXYI SEGMENT="1">
                <TNR>123453</TNR>
                <ORT>1000</ORT>
                
                <ITEM SEGMENT="1">
                    <N11>003</N11>
                    <AR>170</AR>
                </ITEM>
                
                <ITEM SEGMENT="1">
                    <N11>003</N11>
                    <AR>17.40</AR>
                </ITEM>
                
                <ITEM SEGMENT="1">
                    <N11>999</N11>
                    <AR>17.40</AR>
                    <subnode SEGMENT="1">
                        <field1> 13</field1>
                    </subnode>
                </ITEM>
                <ITEMNEW SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                    <PQC>NU</PQC>
                    <QTY>3456</QTY>
                    <NUM/>
                    <ASCD/>
                </ITEMNEW>
            </BXYI>
            <BXYI SEGMENT="1">
                <TNR>123453</TNR>
                <ORT>1000</ORT>
                <ITEM SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>99</N11>
                    <AR>1.7</AR>
                    <subnode SEGMENT="1">
                        <field1> 13</field1>
                    </subnode>
                </ITEM>
                <ITEMNEW SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                    <PQC>NU</PQC>
                    <QTY>3456</QTY>
                    <NUM/>
                    <ASCD/>
                </ITEMNEW>
            </BXYI>
            <BXYI SEGMENT="1">
                <TNR>123453</TNR>
                <ORT>1000</ORT>
                <ITEM SEGMENT="1">
                    <N11>6789</N11>
                    <AR>035</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03424</AR>
                </ITEM>
                <ITEM SEGMENT="1">
                    <N11>003</N11>
                    <AR>170</AR>
                    <subnode SEGMENT="1">
                        <field1> 13</field1>
                    </subnode>
                </ITEM>
                <ITEMNEW SEGMENT="1">
                    <N11>6789</N11>
                    <AR>03</AR>
                    <PQC>NU</PQC>
                    <QTY>3456</QTY>
                    <NUM/>
                    <ASCD/>
                </ITEMNEW>
            </BXYI>
        </BXYH>
    </DOC>
</D02X001>

XSLT I used

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:key name="item-by-N11-AR" match="BXYI/ITEM" composite="yes" use="N11, AR"/>

  <xsl:template match="BXYI/ITEM[not(key('item-by-N11-AR', (N11, AR))[subnode])][not(. is key('item-by-N11-AR', (N11, AR), ..)[1])]"/>
  <xsl:template match="BXYI/ITEM[key('item-by-N11-AR', (N11, AR))[subnode]][not(. is key('item-by-N11-AR', (N11, AR), ..)[subnode][1])]"/>


  <xsl:mode on-no-match="shallow-copy"/>


</xsl:stylesheet>




Solution

  • Test whether

      <xsl:template match="BXYI/ITEM[not(key('item-by-N11-AR', (N11, AR), ..)[subnode])][not(. is key('item-by-N11-AR', (N11, AR), ..)[1])]"/>
      <xsl:template match="BXYI/ITEM[key('item-by-N11-AR', (N11, AR), ..)[subnode]][not(. is key('item-by-N11-AR', (N11, AR), ..)[subnode][1])]"/>
    

    fixes the problem, I think I forgot to add .. as the third key function argument in an earlier attempt to restrict key searches to BXYI element subtrees.