I have multiple XML files which consists of change tracking attribute <atict:add>
or <atict:del>
.
Objective:
- if XML file consists of an element
CT="ACCEPT"
then accept/print all tags with <atict:add>
and ignore <atict:del>
- if XML file consits of an element
CT="REJECT"
then accept/print all tags with <atict:del>
and ignore <atict:accept>
Sample XML:
<?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>
<?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>
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 <atict:add>1456790
</atict:add></PARA>
<?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:add>1456790
</atict:add></PARA>
How can I add the CT in the XSLT with an if condition to satisfy the criteria?
The sample stylesheet below does the job. See the comments for explanations.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:atict="http://www.arbortext.com/namespace/atict"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:variable name="CT_stat">
<xsl:choose>
<xsl:when test="DM/@CT = 'ACCEPT'">1</xsl:when>
<xsl:when test="DM/@CT = 'REJECT'">0</xsl:when>
<xsl:otherwise>2</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- template matching for atict:del and atict:add, retaining
them or deleting them based on $CT_stat variable -->
<xsl:template match="atict:del">
<xsl:choose>
<xsl:when test="$CT_stat=1"/>
<xsl:when test="$CT_stat=0">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="atict:add">
<xsl:choose>
<xsl:when test="$CT_stat=1">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:when test="$CT_stat=0"/>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Or simply:
<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>