XSLT Stylesheet: Changing text to upper case

60,838

Solution 1

XSLT 2.0 has fn:upper-case() and fn:lower-case() functions. However in case you are using of XSLT 1.0, you can use translate():

<xsl:template match="/">
  <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
  <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
  <xsl:value-of select="translate(doc, $smallcase, $uppercase)" />
</xsl:template>

Solution 2

You can use the translate() function in XSLT 1.0:

<xsl:value-of select="translate(//some-xpath,
                                'abcdefghijklmnopqrstuvwxyz',
                                'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />

If you're lucky enough to have access to XSLT 2.0, you can use the upper-case() function:

<xsl:value-of select="upper-case(//some-xpath)"/>

See the XPath function reference page for more details.

Solution 3

XPath 2.0 has fn:upper-case(), which also does Unicode correct case mappings.

Solution 4

Use an Assembly like this:

<msxsl:script implements-prefix="user" language="C#">
<!--{%assembly%}-->
<![CDATA[  

public string ToUpper(string stringValue)
{
    string result = String.Empty;

    if(!String.IsNullOrEmpty(stringValue))
    {
      result = stringValue.ToUpper(); 
    }

    return result;
}
]]>
</msxsl:script>

Call it as follows: select="user:ToUpper(//root/path)"

This can be used in 1.0 or 2.0.

Share:
60,838

Related videos on Youtube

Admin
Author by

Admin

Updated on April 29, 2020

Comments

  • Admin
    Admin almost 4 years

    I am using an XSLT stylesheet to create an Excel document from an XML file. One of the values that I am pulling in I want to display as upper case. How is this possible?

  • AakashM
    AakashM over 14 years
    Asker's name looks vaguely French... what happens to é ? (sorry, I couldn't resist...)
  • Welbog
    Welbog over 14 years
    @AakashM: That's the problem with the translate() function. You have to specify all of these things yourself. upper-case() is a much better option but it's not supported widely enough.
  • Welbog
    Welbog over 14 years
    +1, though it's the same strategy as my answer keeping variables of the letters is certainly a more reusable solution.
  • Daniel Haley
    Daniel Haley over 12 years
    I believe the OP was creating an Excel file; not HTML.
  • Azhar Khorasany
    Azhar Khorasany over 11 years
    Absolutely beautiful and easy :)
  • iconoclast
    iconoclast over 10 years
    You can create Excel files by way of HTML. I believe it will honor CSS, but I'm not 100% sure.
  • Pekka
    Pekka over 9 years
    The solutions does not permit case-insensitive comparison of text elements in the document - which is often the reason for forcing text into a single, consistent, case. This solution will only apply to the output rendering of the result.
  • rainabba
    rainabba over 8 years
    "I am using an XSLT stylesheet" is the key here. @iconoclast Yes you can (not a shameless plug, I swear): jqueryscript.net/table/…