2

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

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Greatt it worked fine. Thanks. I have a question though. There might be a scneario where formatid element is not there ( like in person1 instead of empty element, the element itself is not present). Even in that scenario, I shall present formatid. how to achieve that. Sorry I missed that point earlier
@mnvbrtn See the addition to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.