Input:
<Data>
<person>
<id>123456</id>
<formatid></formatid>
</person>
<person>
<id></id>
<formatid>****89</formatid>
</person>
<person>
<id>234567</id>
<formatid>****89</formatid>
</person>
</Data>
Output:
<Data>
<person>
<formatid>****56</formatid>
</person>
<person>
<formatid>****89</formatid>
</person>
<person>
<formatid>****67</formatid>
</person>
</Data>
Need to convert the input xml to output. Conditions are
- If the request has id, always send the last two digits, it doesn't matter formatid has a value or not (person 1 and 3).
- If the request has blank id , then send the format id as is. (person 2)
My stylesheet
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="id">
<xsl:variable name="clearid" select="./text()"/>
<xsl:choose>
<xsl:when test="$clearid != ''">
<xsl:variable name="idLen" select="string-length($clearid)"/>
<xsl:variable name="star" select="translate($clearid, '0123456789','**********')"/>
<xsl:variable name="idstar" select="concat( substring($star, 1, $idLen - 1), substring($clearid, $idLen - 1))"/>
<xsl:element name="formatid">
<xsl:value-of select="$idstar"/>
</xsl:element>
</xsl:when>
<xsl:when test="$clearid = ''">
<xsl:element name="formatid">
<xsl:value-of select="following-sibling::formatid"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="formatid"/>
<!-- copy the rest of the message as is -->
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When I use this everything working fine except the person 2. because it is already removed