xslt-1.0ibm-datapower

Dynamically replace matched special characters


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>

Solution

  • Assuming that the file listing special characters should actually look like this:

    chars.xml

    <chars>
      <char>
        <value>&#128;</value>
      </char>
      <char>
        <value>&#129;</value>
      </char>
      <char>
        <value>&#130;</value>
      </char>
    </chars>
    

    you could use the following stylesheet:

    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="*"/>
    
    <xsl:param name="charfile" select="document('chars.xml')"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:variable name="search-chars">
        <xsl:for-each select="$charfile/chars/char">
            <xsl:value-of select="value"/>
        </xsl:for-each>
    </xsl:variable>
    
    <xsl:variable name="replace-chars">
        <xsl:for-each select="$charfile/chars/char">
            <xsl:text>*</xsl:text>
        </xsl:for-each>
    </xsl:variable>
    
    <xsl:template match="text()">
        <xsl:value-of select="translate(., $search-chars, $replace-chars)"/>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Given an input of:

    XML

    <root>
      <child1>abc&#128;</child1>
      <child2>def&#129;</child2>
      <child3>hello</child3>
    </root>
    

    the output will be:

    Result

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
      <child1>abc*</child1>
      <child2>def*</child2>
      <child3>hello</child3>
    </root>