Shravan Vishwanathan
Shravan Vishwanathan

Reputation: 113

Remove element in XML on condition

I have multiple XML files which consist of change tracking attribute or .

Objective:

The XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<DM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:atict="http://www.arbortext.com/namespace/atict" CT="ACCEPT">  
  <PARA>abcd 
    <atict:del>efghi</atict:del>
    <atict:add>1456790</atict:add>
  </PARA>
</DM>

Desired output XML after processing

<?xml version="1.0" encoding="UTF-8"?>
<DM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:atict="http://www.arbortext.com/namespace/atict" CT="ACCEPT">
  <PARA>abcd 1456790 </PARA>
</DM>

XSLT:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:atict="http://www.arbortext.com/namespace/atict">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>    
  <xsl:template match="atict:del[ancestor::DM/@CT='ACCEPT']"/>
  <xsl:template match="atict:add[ancestor::DM/@CT='REJECT']"/>
</xsl:stylesheet>

With my XSLT I get the element tags. I only need the values inside the respective tag after processing.

Upvotes: 0

Views: 690

Answers (1)

uL1
uL1

Reputation: 2167

I only need the values inside the respective tag after processing.

Because your identity transform template copy the element once again.

Define yourself two further! templates:

<xsl:template match="atict:del">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="atict:add">
    <xsl:apply-templates/>
</xsl:template>

Depending on the internal priority of the templates the xslt processor either takes the "delete"-templates or the (new one) "content-only"-templates.

Upvotes: 1

Related Questions