mnvbrtn
mnvbrtn

Reputation: 568

Replace the element using xslt

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

  1. 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).
  2. 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

Upvotes: 2

Views: 1221

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

Couldn't you do simply:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="formatid[string(../id)]">
    <xsl:copy>
        <xsl:text>****</xsl:text>
        <xsl:value-of select="substring(../id, string-length(../id) - 1)"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="id"/>

</xsl:stylesheet>

Added:

There might be a scneario where formatid element is not there

Then I would do:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Data">
    <xsl:copy>
        <xsl:for-each select="person">
            <xsl:copy>
                <formatid>
                    <xsl:choose>
                        <xsl:when test="string(formatid)">
                            <xsl:value-of select="formatid"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>****</xsl:text>
                            <xsl:value-of select="substring(id, string-length(id) - 1)"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </formatid>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions