Updated April 7, 2023
Introduction to XSLT Translate
The following article provides an outline for XSLT Translate. The translate function is a case conversion that evaluates a string and a set of characters to translate and returns the translated string. It can replace or remove individual characters with other single characters. Translate() replaces all the occurrence of characters specified in the first argument with the replacement character specified in the second argument and returns the modified value of the string. Translate is available with XPATH 1.0.
Syntax:
The function definition or syntax is given here with the parameters accepted by the function.
Translate(string, from characters, to characters)
This function has three parameters:
- string: The character string to evaluate.
- from characters: The characters going to be replaced.
- to characters: The characters used for replacement purpose.
If any of the argument is NULL, the function returns null.
How does Translate Function Work in XSLT?
The job of this function is to replace some content on the declared entity. The translate function gives mapping techniques by mapping characters in the first argument, and the last two strings provide a schema mapping, and the function is case-sensitive. But the ‘s behaviourtranslate function’s behaviour is similar to replace() functions where the latter replaces all matching characters.
Let’s see a few samples on replacing the characters with different cases.
Case 1: Dates and string of characters.
translate('1995/02/03', '/', '-')
1995-02-03
translate('html page', 'pamt', 'PAMT')
HTML PAGE
translate(' html page', 'pamt ', 'P')
Page
translate(' html page', 'pamt', '')
age
translate(' html page', 'mnop', 'MNOP')
html page
translate('', 'pamt ', 'P')
zero-length string
translate((), 'pamt ', 'P')
zero-length string
Case 2: Replacing a few characters in a string.
fn:translate( 'Welcome to the course!', 'me', 'ME'
)
results in: "WElcoME to the coursE!:”
Case 3: Mechanism working for double-quotes.
<xsl:value-of select='translate($textdemo, """", "_")'/>
To remove extra characters, the technique we use here is normalize() along with the translate() function.
<xsl:template match="/">
<xsl:variable name="white"
select=" '---Destroying --water—is destroying future generations right now---' "/>
<xsl:value-of select="translate(normalize-space(
translate($white,'- ',' -')),'- ',' -')"/>
</xsl:template>
So, the above template gives the output as:
Destroying -water-is destroying future generations right now.
Now we have little confusion with replacing and translate() function.
The replace-function replaces one string with the other string. It’s like replacing ‘mn’ with ‘xy’; we get xycmcb for the string ‘mncmcb’. Whereas in case translate() translate ‘ab’ with ‘xy’, we get xycxcy for the string “mncmcn”.
Sometimes we can directly strip out any occurrences of the query set from the original string.
This principle can be applied with the punctuation marks (!).
<xsl:value-of select="$str"/><xsl:text> does </xsl:text>
<xsl:if
test="string-length(translate(substring($str,string-length($str)),
',.:;?!',''))
!= 0">
Examples of XSLT Translate
Given below are the examples of XSLT Translate:
Example #1
Generating the following XML document for the bookstore.
XML:
<?xml version="1.0"?>
<bookstore>
<book ISBN="0123456001">
<title>Java for Dummies</title>
<author>Tan Ah Teck</author>
<category>Programming</category>
<year>2009</year>
<edition>7</edition>
<price>19.99</price>
</book>
<book ISBN="0123456002">
<title>More Java for Dummies</title>
<author>Tan Ah Teck</author>
<category>Programming</category>
<year>2008</year>
<price>25.99</price>
</book>
</bookstore>
XSL:
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="new.xsl"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
translate("bar","abc","ABC") =
<xsl:value-of select='translate("minus","nus","NUS")'/>
<br/>translate("--mmm--","nus-","NUS") =
<xsl:value-of select='translate("--mmm--","nus-","NUS")'/>
</xsl:template>
</xsl:stylesheet>
Explanation:
- In the above code, the translation mapping contains ‘nus’ respectively. But the removal string, which is the third argument, resets the mapping.
Output:
Example #2
XML:
<?xml version="1.0"?>
<base summary="few •^#x6;summary"/>
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/base">
<result>
<xsl:value-of select="translate(@summary,' •^#x6;',' ')"/>
</result>
</xsl:template>
</xsl:stylesheet>
Explanation:
- The above code translates a special character to give the below output.
Output:
Example #3
XML:
<coding>
<statement>Bug 1</statement>
<statement>
Bug 2
</statement>
</coding>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="coding">
<xsl:value-of select="concat('length: ',string-length(.))" />
<xsl:if test="contains(.,'heavy')">
<xsl:text>heavy: yes!</xsl:text>
</xsl:if>
<xsl:if test="starts-with(.,'Bug2')">
<xsl:text>well, starts with "Bug2"</xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space(.)" />
<xsl:value-of select="translate(.,'mnop','MNOP')" />
</xsl:template>
</xsl:stylesheet>
Explanation:
- The above code does normalization space before the translation mechanism.
Output:
Example #4
XML:
<?xml version="1.0"?>
<Category>
<Churches>
<churchname>Antioch Community</churchname>
<churchsize>Big</churchsize>
<builtyear>1890-11-12</builtyear>
</Churches>
<Churches>
<churchname>Bay Area Christian</churchname>
<churchsize>3200</churchsize>
<builtyear>1991-1-12</builtyear>
</Churches>
<Churches>
<churchname>Calvary Chapel Chino Valley</churchname>
<churchsize>Small</churchsize>
<builtyear>1901-8-09</builtyear>
</Churches>
</Category>
XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates
select="//text()[normalize-space(.)]" />
</body>
</html>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="upcase"
select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="lowcase"
select="'abcdefghijklmnopqrstuvwxyz'" />
The source is
<span
style="background-color: #f47d2d;">
<xsl:value-of select="." />
</span>
was changed to
<span
style="background-color: #c39797;">
<xsl:value-of select="translate(., $lowcase, $upcase)" />
<br />
</span>
</xsl:template>
</xsl:stylesheet>
Explanation:
- The above stylesheet use translate() function to change the given text in lowercase to uppercase. We have defined two variables, lowercase and uppercase, and created two templates that change the string’s case.
Output:
Example #5
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="ff.xsl"?>
<Cosmetics >
<Categories cc = "1" >
<skin>Toners </skin>
<skin>Mists</skin>
<skin>Night Cream </skin>
</Categories>
<Categories cc = "2" >
<Personalcare>Rollers and Curlers </Personalcare>
<Personalcare>Hairfall and thining </Personalcare>
<Personalcare>Lotions and creams </Personalcare>
</Categories>
</Cosmetics>
XSL:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<hmtl>
<head>
<Header>Products Online</Header>
</head>
<xsl:apply-templates/>
</hmtl>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Cosmetics">
<table>
<thead>
<tr>
<th>Header</th>
<th>Author</th>
<th>Greater/Less</th>
</tr>
</thead><tbody>
<xsl:for-each select="Categories">
<tr>
<xsl:apply-templates select="current()"/>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
<xsl:template match="Categories">
<xsl:apply-templates select="skin"/>
<xsl:apply-templates select="Personalcare"/>
<xsl:apply-templates select="skin" mode="skin"/>
</xsl:template>
<xsl:template match="skin">
<div class="cosmetic">
<div class="translate">
<xsl:value-of select="translate(current(),' ', 'Lakme')"/>
</div>
<div class="replace">
<xsl:value-of select="replace(current(),' ', 'Lakme')"/>
</div>
</div>
</xsl:template>
<xsl:template match="Personalcare">
<div1>
<xsl:value-of select="current()"/>
</div1>
</xsl:template>
<xsl:template match="Personalcare" mode ="personalcare">
<div class="cosmetic1">
<xsl:value-of select="."/>
</div>
</xsl:template>
</xsl:transform>
Explanation:
- Here we are supposed to convert a string specified in the element personal care, and they must be replaced with the string ‘Lakme’. To achieve this, the translate function supplies the translation string, and when we execute the above code, it gives the following result.
Output:
Advantages of XSLT Translate
Given below are the advantages mentioned:
- This function can strip certain characters like whitespaces from the input.
- Translate being a versatile function often used for missing string-processing in XSLT.
- Even it removes all non-numeric function declared in the string.
- It does one-to-one substitutions in a single operation.
Conclusion
Finally, we have presented the XSLT translate() function and discussed their operations with an example. In summary, we have seen how to use this function by several single-characters in a single operation.
Recommended Articles
This is a guide to XSLT Translate. Here we discuss the introduction, how does translate function work in XSLT? and examples respectively. You may also have a look at the following articles to learn more –