XML/XSLT stylesheet/translate
translate function
File: Data.xml
<poem>
<verse>line 1</verse>
<verse>
line 2
</verse>
</poem>
File: Transform.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="verse">
<xsl:value-of select="concat("length: ",string-length(.))" />
<xsl:if test="contains(.,"light")">
<xsl:text>light: yes!</xsl:text>
</xsl:if>
<xsl:if test="starts-with(.,"Seest")">
<xsl:text>Yes, starts with "Seest"</xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space(.)" />
<xsl:value-of select="translate(.,"abcde","ABCD")" />
</xsl:template>
</xsl:stylesheet>
Output:
length: 6line 1lin 1
length: 13line 2
lin 2
Use translate() function and text() function to change the letter case
File: Data.xml
<?xml version="1.0"?>
<Employees>
<Person>
<FirstName>A</FirstName>
<LastName>B</LastName>
<DateOfBirth>2008-12-12</DateOfBirth>
</Person>
<Person>
<FirstName>C</FirstName>
<LastName>D</LastName>
<DateOfBirth>2008-11-11</DateOfBirth>
</Person>
<Person>
<FirstName>E</FirstName>
<LastName>F</LastName>
<DateOfBirth>2008-10-10</DateOfBirth>
</Person>
</Employees>
File: Transform.xslt
<?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="upper"
select=""ABCDEFGHIJKLMNOPQRSTUVWXYZ"" />
<xsl:variable name="lower"
select=""abcdefghijklmnopqrstuvwxyz"" />
The input
<span
style="background-color: #c0c0c0;">
<xsl:value-of select="." />
</span>
was translated to
<span
style="background-color: #ffd700;">
<xsl:value-of select="translate(., $lower, $upper)" />
<br />
</span>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
The input
<span style="background-color: #c0c0c0;">A</span>
was translated to
<span style="background-color: #ffd700;">A<br></span>
The input
<span style="background-color: #c0c0c0;">B</span>
was translated to
<span style="background-color: #ffd700;">B<br></span>
The input
<span style="background-color: #c0c0c0;">2008-12-12</span>
was translated to
<span style="background-color: #ffd700;">2008-12-12<br></span>
The input
<span style="background-color: #c0c0c0;">C</span>
was translated to
<span style="background-color: #ffd700;">C<br></span>
The input
<span style="background-color: #c0c0c0;">D</span>
was translated to
<span style="background-color: #ffd700;">D<br></span>
The input
<span style="background-color: #c0c0c0;">2008-11-11</span>
was translated to
<span style="background-color: #ffd700;">2008-11-11<br></span>
The input
<span style="background-color: #c0c0c0;">E</span>
was translated to
<span style="background-color: #ffd700;">E<br></span>
The input
<span style="background-color: #c0c0c0;">F</span>
was translated to
<span style="background-color: #ffd700;">F<br></span>
The input
<span style="background-color: #c0c0c0;">2008-10-10</span>
was translated to
<span style="background-color: #ffd700;">2008-10-10<br></span></body>
</html>
Use translate function with if statement
File: Data.xml
<poem>
<a>line 1</a>
<b>line 1</b>
<c>line 1</c>
<d>
line 1
</d>
</poem>
File: Transform.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="a">
<xsl:if test=". = "line 1"">
1. a = "line 1"
</xsl:if>
<xsl:if test=". = ../b">2. a = ../b</xsl:if>
<xsl:if test=". = ../c">3. a = ../c</xsl:if>
<xsl:if test=". != ../c">4. a != ../c</xsl:if>
<xsl:if
test="translate(.,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ") = translate(../c,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")">
5. a = ../c (ignoring case)
</xsl:if>
<xsl:if test=". = ../d">6. a = ../d</xsl:if>
<xsl:if test=". = normalize-space(../d)">
7. a = normalize-space(../d)
</xsl:if>
</xsl:template>
<xsl:template match="b|c|d" />
</xsl:stylesheet>
Output:
1. a = "line 1"
2. a = ../b3. a = ../c
5. a = ../c (ignoring case)
7. a = normalize-space(../d)