Recently, while working on XML source component of SSIS Kingswaysoft, we got a requirement that there could be a scenario in which a specific tag would be missing in the XML source which is mandatory.
We were supposed to add the tag if it’s missing in the input XML.
There could me many other ways of achieving this. Below is the solution of achieving this using XSLT.
Input XML:
<REQUEST>
<CONSUMER>
<CID1>AAAA</CID1>
<CID2>BBBB</CID2>
</CONSUMER>
</REQUEST>
Using XSLT, we’ll check if the input XML contains CINDPAYS tag inside CONSUMER tag or not. If it exists, then do nothing, otherwise add empty CINDPAYS tag.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:param name="ename">c</xsl:param>
<xsl:param name="evalue">cccc</xsl:param>
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="CONSUMER">
<xsl:copy>
<xsl:apply-templates/>
<xsl:if test="not(CINDPAYS)">
<CINDPAYS>
<CPER/>
<COUTBAL/>
<CAPPLIMIT/>
<CLAP/>
<CINSTAMT/>
<CLPD>
<CLPDDD/>
<CLPDMM/>
<CLPDYY/>
</CLPD>
<CNDDATE>
<CNDDD/>
<CNDMM/>
<CNDYY/>
</CNDDATE>
<CPDB/>
<CPAYSTS/>
</CINDPAYS>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Below is the output XML:
<REQUEST>
<CONSUMER>
<CINDPAYS>
<CPER/>
<COUTBAL/>
<CAPPLIMIT/>
<CLAP/>
<CINSTAMT/>
<CLPD>
<CLPDDD/>
<CLPDMM/>
<CLPDYY/>
</CLPD>
<CNDDATE>
<CNDDD/>
<CNDMM/>
<CNDYY/>
</CNDDATE>
<CPDB/>
<CPAYSTS/>
</CINDPAYS>
<CID1>AAAA</CID1>
<CID2>BBBB</CID2>
</CONSUMER>
</REQUEST>
Hope it helps !!