Shravan Vishwanathan
Shravan Vishwanathan

Reputation: 113

Change tracking in XML with condition

I have an XML with change tracking.

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> .

But, in case the XML does not contain any CT element then it should insert ADD and DEL elements accordingly(wherever <atict:add> and <atict:del> are present.

I am able to achieve the first part of the question but not the second part.

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>
</DM>

XSLT 1.0:

<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:template match="atict:add">
<xsl:element name="ADD">
<xsl:value-of select='.'/>
</xsl:element>
</xsl:template>
<xsl:template match="atict:del">
<xsl:element name="DEL">
<xsl:value-of select='.'/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

The problem is i am getting ADD/DEL element when CT element is present. I don't require ADD ,DEL element when CT=ACCEPT or CT=REJECT element is present in the result XML.

Could you please help me to put a condition if possible.

Thanks for your help.

Upvotes: 0

Views: 89

Answers (2)

Shravan Vishwanathan
Shravan Vishwanathan

Reputation: 113

After searching in the web,I believe this may also be the XSLT:

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:add[not(ancestor::DM/@CT='ACCEPT')]"  >
<xsl:element name="ADD">
<xsl:value-of select='.'/>
</xsl:element>
</xsl:template>
<xsl:template match="atict:del[not(ancestor::DM/@CT='REJECT')]">
<xsl:element name="DEL">
<xsl:value-of select='.'/>
</xsl:element>
</xsl:template>
<xsl:template match="atict:del[ancestor::DM/@CT='ACCEPT']"/>
<xsl:template match="atict:add[ancestor::DM/@CT='REJECT']"/>
</xsl:stylesheet>

Upvotes: 0

potame
potame

Reputation: 7905

Something like:

<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:template match="atict:add[ancestor::DM/@CT='ACCEPT']">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="atict:del[ancestor::DM/@CT='REJECT']">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="atict:add[not(ancestor::DM/@CT)]">
    <xsl:element name="ADD">
      <xsl:value-of select='.'/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="atict:del[not(ancestor::DM/@CT)]">
    <xsl:element name="DEL">
      <xsl:value-of select='.'/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

should do the job properly.

Basically you need to handle the atict:del or atict:add tag separately when then are required to be removed or kept, and also changed to <ADD> or <DEL> elements.

See it working live here: http://xsltransform.net/3NSSEv2

Upvotes: 1

Related Questions