I have a special character file (with list of special characters). I have to read that file and check if the incoming request has any of the special character, if so: replace that with constant else: move it as is
in xslt 1.0 Tried with below code.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:regex="http://exlt.or/regular-expressions">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="charfile" select="document('chars.xml')"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="checkAndReplace">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="checkAndReplace">
<xsl:param name="text"/>
<xsl:for-each select="$charfile/chars/char">
<xsl:if test="contains($text,./value/text())">
<xsl:copy-of select="translate($text,./value/text(),'*')"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
chars.xml :
<?xml version="1.0" encoding="utf-8"?>
<chars>
<char>
<value>@#128;</value>
</char>
<char>
<value>@#129;</value>
</char>
<char>
<value>@#130;</value>
</char>
</chars>
INPUT:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child1>abc@#128;</child1>
<child2>def@#129;</child2>
<child3>hello</child3>
</root>
Output Required:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child1>abc*</child1>
<child2>def*</child2>
<child3>hello</child3>
</root>