EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials XML Tutorial XSLT replace

XSLT replace

Updated April 18, 2023

XSLT replace

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 &amp; Prediction &amp; </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(),'&amp;','and')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Explanation:

The above code replaces the current node element with the &amp with the string “and”.

Output:

XSLT Replace 1

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:

XSLT Replace 2

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:

XSLT Replace 3

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:

XSLT Replace 4

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 –

  1. XML ampersand
  2. XML HttpRequest 
  3. XML Element
  4. XML XSD
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW