Updated April 18, 2023
Definition of XSLT replace Function
XSLT replace is deterministic and does string manipulation that replaces a sequence of characters defined inside a string that matches an expression. In simple terms, it does string substitution in the specified place by replacing any substrings. Fn: replace function is not available in XSLT1.0 and it is well treated in XSLT2.0. XSLT2.0 handles more complicated operations.
Syntax:
<xsl:value-of select=" replace ($input, $pattern, $replacement)”/>
Replace Function takes three parameters namely input as text, pattern a string fragment, replacement string.
<xsl:when test="$context = '' or $replace = ''or not($replace)" >
So, the following sections list the ways how String Replacement is established based on user requirements.
How to replace function works in XSLT?
The Replace function works by writing an XSLT template for this function and calls this replace whenever replacement of the string action is required. This function is very similar to regex-group (). If the specified input string has no substring that matches the defined regular expression, it gives the output as a single string similar to the input string. As defined in the syntax, the dollar sign references the matches with the expressions as well as sub-expressions. The expressions are a string of text and the sub-expressions are shown with parenthesis. Let’s have a look at this statement.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:output method="text">
<xsl:template match="/">
<xsl:text> The demo of match '</xsl:text>
<xsl:value-of select="replace('contact me at 321-898-555-7680, hello', '({0-9}{3})-({0-9}{3})-({0-9}{4}}', '$0')"/>
<xsl:text>' contains a similar ! </xsl:text>
<xsl:text> The matching with the expression : </xsl:text>
</template>
The output is displayed as
'contact me at 321-898-555-7680, hello'
contact me (321) 555-7680 hello.
The function returns the value of the first string passed as an argument with every substring defined by the regular structure in the second string and finally, it is replaced by the third argument.
For a given string with a regular expression
<xsl:variable name="aaa" select="'My demo'"/>
The Specified replacement String of above statement is
<xsl:value-of select="replace($aaa, 'My', 'demo')"/>
Here it replaces part of a string that matches a declared regular expression.
replace("Historicalevent", "tor", "*") - Which gives output as His*icalevent
replace("Historicalevent ", "i", "") - Gives output as Hstorcalevent
<xsl:value-of select="string:replace('m n p',' ',' go to ')" />
M go to n go to p
The sample code shows how it works.
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Examples
Let us examples of XSLT replace.
Example #1: Simple Example with Replace function
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<Content>
<Summary>Types of analysis in data Mining are classification & Prediction & </Summary>
</Content>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="Content">
<xsl:for-each select="Summary">
<xsl:value-of select=" replace(current(),'&','and')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Explanation:
The above code replaces the current node element with the & with the string “and”.
Output:
Example #2: Replica of the function file.
XML
<?xml version="1.0"?>
<SNO>
<Title>SQL Functions Defined</Title>
<Launched>2006-07-16</Launched>
<fname type="new">AVG-mathematical()</fname>
<fname type="new">count()</fname>
<fname type="new">Aggregate()</fname>
<fname type="new">Scalar()</fname>
<fname type="new">Round()</fname>
<fname type="new">Format()</fname>
<fname type="alphab">UCASE()</fname>
<fname type="alphab">Lcase()</fname>
</SNO>
XSL
<?xml version ="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="fnames">
<xsl:element name="SNO">
<xsl:element name="Title">
SQL Functions Defined
</xsl:element>
<xsl:element name="Launched">
<xsl:value-of select="count()" />
</xsl:element>
<xsl:apply-templates select="fname" />
</xsl:element>
</xsl:template>
<xsl:template match="fname">
<xsl:copy>
<xsl:value-of select="replace(name, '^hh:', '')" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Explanation
The first argument of the function is the name element defined in the value-of statement. Second argument is the regular expression with ^hh. It produces same result as an XML file with the function.
Output:
Example #3: With special characters
<?xml version="1.0" encoding="UTF-8"?>
<record>
<link>[www]/XSLTimages/[Norwayoslo].png</link>
</record>
XSL file
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template name="string-replace">
<xsl:param name="text" />
<xsl:param name="pattern" />
<xsl:param name="replace-with" />
<xsl:choose>
<xsl:when test="contains($text, $pattern)">
<xsl:value-of select="substring-before($text, $pattern)" />
<xsl:value-of select="$replace-with" />
<xsl:call-template name="string-replace">
<xsl:with-param name="text" select="substring-after($text, $pattern)" />
<xsl:with-param name="pattern" select="$pattern" />
<xsl:with-param name="replace-with" select="$replace-with" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="link">
<xsl:variable name="Imagelink"><xsl:value-of select="."/></xsl:variable>
<xsl:variable name="sample">
<xsl:call-template name="string-replace">
<xsl:with-param name="text" select="$Imagelink" />
<xsl:with-param name="pattern" select="'XSLTimages'" />
<xsl:with-param name="replace-with" select="'XSLTimages/_R'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="dodemo">
<xsl:call-template name="string-replace">
<xsl:with-param name="text" select="$sample" />
<xsl:with-param name="pattern" select="'.png'" />
<xsl:with-param name="replace-with" select="'_png.png'" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$dodemo" />
</xsl:template>
</xsl:stylesheet>
Explanation
A very simple approach in XSLT2.0 with the values for replacing is taken. Here I have taken a string png to replace and the literal _R. The result should look like this.
Output:
Example #4: Replacing multiple strings in the content
XML
<?xml version="1.0" encoding="UTF-8"?>
<Alcohollist>
<alcohol magic="Cognack Rum">
<winery>Tequila</winery>
<product>Barley and corn</product>
<sale>1991</sale>
<content>Well-textured product, Americans flavors.</content>
<taxes>
<lprize>13.4</lprize>
<GST>19.50</GST>
<bottle>124.00</bottle>
</taxes>
</alcohol>
<alcohol magic="Vermoth">
<winery>port wine</winery>
<product>potatoes and fermented</product>
<sale>1994</sale>
<content>Distilled Beverages flavors, lasting case.</content>
<taxes>
<lprice>15.19</lprice>
<GST>12.99</GST>
<bottle>160.50</bottle>
</taxes>
</alcohol>
<alcohol magic="Vermoth">
<winery>port wine</winery>
<product>potatoes and fermented</product>
<sale>1994</sale>
<content>Distilled Beverages flavors, lasting case.</content>
<taxes>
<lprice>15.19</lprice>
<GST>12.99</GST>
<bottle>160.50</bottle>
</taxes>
</alcohol>
</Alcohollist>
XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template name="globalReplace">
<xsl:param name="outputString"/>
<xsl:param name="target"/>
<xsl:param name="replacement"/>
<xsl:choose>
<xsl:when test="contains($outputString,$target)">
<xsl:value-of select=
"concat(substring-before($outputString,$target),
$replacement)"/>
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString"
select="substring-after($outputString,$target)"/>
<xsl:with-param name="target" select="$target"/>
<xsl:with-param name="replacement"
select="$replacement"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$outputString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select=" replace(current(),'Beverages;','cool')"/>
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="."/>
<xsl:with-param name="target" select="'flavors'"/>
<xsl:with-param name="replacement" select="'flavors'"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="@*|*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Explanation
The above template replaces the character string with the replacement. Therefore the result of calling this stylesheet is shown below. So when I type on the browser I get the following response.
Output:
Advantages
- With the help of the XSLT2.0 replace () function removes undesired characters from a string in powerful regular expressions. XSLT replaces corresponding single characters, not the entire string.
- The dollar sign ($) interprets the rightmost characters in the string as literal. And if the single-digit is higher than the sub-expressions the digit or character is replaced with an empty string.
Conclusion
Somehow, we have discussed and concluded on replace function. This function will get deep specification to stay grip on it and highlighted how to walk through the replace strings with the regular expressions and create Stylesheet functions to do with templates.
Recommended Articles
This is a guide to XSLT replace. Here we discuss the Introduction, syntax, How to replace function works in XSLT?, and examples. You may also have a look at the following articles to learn more –