Search code examples
stringxsltxslt-1.0xslt-2.0xslt-grouping

Add '(apostrophe) as a valid character in XSLT template


I am working on a project there they have given a list of valid characters which should be passed as it is and any other characters other than them has to be replaced with ?(question mark). My company software does not allows to use replace function, so i have to use translate method.

I am able to add all the valid characters except '(apostrophe). How to include apostrophe also as a valid character in variable vAllowedSymbols.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="vAllowedSymbols" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/-?:(),.+ '"/>
<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="text()">
    <xsl:value-of select="
        translate(
            .,
            translate(., $vAllowedSymbols, ''),
            ''
            )
        "/>
</xsl:template>

Input:

<t>
<Name>@O'Niel</Name>
<Name>St: Peter</Name>
<Name>A.David</Name>
</t>

Output:

<t>
<Name>?O'Niel</Name>
<Name>St: Peter</Name>
<Name>A.David</Name>
</t>

Solution

  • Use

    <xsl:variable name="vAllowedSymbols">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/-?:(),.+ '</xsl:variable>