Home > other >  XSLT : Remove XML one node based on element value & remove an element in another node
XSLT : Remove XML one node based on element value & remove an element in another node

Time:02-11

I'm facing the following problem :

The XML input is :

<root>
    <ITEM_DATA>
        <DOC_NUMBER>0021182064</DOC_NUMBER>
        <ITM_NUMBER>000010</ITM_NUMBER>
        <ITEM_CATEG>ZBOM</ITEM_CATEG>
        <HG_LV_ITEM>000000</HG_LV_ITEM>
    </ITEM_DATA>                

    <ITEM_DATA>
        <DOC_NUMBER>0021182064</DOC_NUMBER>
        <ITM_NUMBER>000020</ITM_NUMBER>
        <ITEM_CATEG>ZAN</ITEM_CATEG>
        <REL_FOR_BI>A</REL_FOR_BI>
        <HG_LV_ITEM>000010</HG_LV_ITEM>
        <ALTERN_ITM>000000</ALTERN_ITM>
    </ITEM_DATA>

    <ITEM_DATA>
        <DOC_NUMBER>0021182064</DOC_NUMBER>
        <ITM_NUMBER>000030</ITM_NUMBER>
        <ITEM_CATEG>ZAN</ITEM_CATEG>
        <REL_FOR_BI>A</REL_FOR_BI>
        <HG_LV_ITEM>000010</HG_LV_ITEM>
        <ALTERN_ITM>000000</ALTERN_ITM>
    </ITEM_DATA>
    
    <ITEM_DATA>
        <DOC_NUMBER>0021182064</DOC_NUMBER>
        <ITM_NUMBER>000040</ITM_NUMBER>
        <ITEM_CATEG>ZTAT</ITEM_CATEG>
        <REL_FOR_BI>A</REL_FOR_BI>
        <ALTERN_ITM>000000</ALTERN_ITM>
    </ITEM_DATA>        
</root>

I want to remove the XML node "ITEM_DATA" having the element "ITEM_CATEG" with value ZBOM. So to do that I apply this XSLT code :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
<xsl:template match="ITEM_DATA[ITEM_CATEG = 'ZBOM']"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

That's works fine. But I also want to remove the element "HG_LV_ITEM" in the other nodes where their value equal to "ITM_NUMBER" value and "ITEM_CATEG" = ZBOM.

The expected output result is :

<root>
    <ITEM_DATA>
        <DOC_NUMBER>0021182064</DOC_NUMBER>
        <ITM_NUMBER>000020</ITM_NUMBER>
        <ITEM_CATEG>ZAN</ITEM_CATEG>
        <REL_FOR_BI>A</REL_FOR_BI>
        <ALTERN_ITM>000000</ALTERN_ITM>
    </ITEM_DATA>

    <ITEM_DATA>
        <DOC_NUMBER>0021182064</DOC_NUMBER>
        <ITM_NUMBER>000030</ITM_NUMBER>
        <ITEM_CATEG>ZAN</ITEM_CATEG>
        <REL_FOR_BI>A</REL_FOR_BI>
        <ALTERN_ITM>000000</ALTERN_ITM>
    </ITEM_DATA>
    
    <ITEM_DATA>
        <DOC_NUMBER>0021182064</DOC_NUMBER>
        <ITM_NUMBER>000040</ITM_NUMBER>
        <ITEM_CATEG>ZTAT</ITEM_CATEG>
        <REL_FOR_BI>A</REL_FOR_BI>
        <ALTERN_ITM>000000</ALTERN_ITM>
    </ITEM_DATA>        
</root>

Can you please share your advices ?

Thanks in advance.

CodePudding user response:

Please try the following XSLT.

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="ITEM_DATA[ITEM_CATEG = 'ZBOM']"/>

    <xsl:template match="HG_LV_ITEM[. = ../ITM_NUMBER][../ITEM_CATEG = 'ZBOM']"/>
</xsl:stylesheet>
  • Related