Use translate() function and text() function to change the letter case : translate « XSLT stylesheet « XML






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>

 








Related examples in the same category

1.translate function
2.Use translate function with if statement