25

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?

ErikE
  • 48,881
  • 23
  • 151
  • 196
  • possible duplicate of [How can I convert a string to upper- or lower-case with XSLT?](http://stackoverflow.com/questions/586231/how-can-i-convert-a-string-to-upper-or-lower-case-with-xslt) – user Nov 29 '14 at 02:22

5 Answers5

55

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>
David Christiansen
  • 5,869
  • 2
  • 36
  • 42
20

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.

Welbog
  • 59,154
  • 9
  • 110
  • 123
  • 3
    Asker's name looks vaguely French... what happens to é ? (sorry, I couldn't resist...) – AakashM Jul 30 '09 at 14:55
  • @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 Jul 30 '09 at 15:12
3

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

Tomalak
  • 332,285
  • 67
  • 532
  • 628
2

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.

mech
  • 2,775
  • 5
  • 30
  • 38
-7

The easiest and cleanest way to achieve case transforms is by the means of CSS.

build a class, like:

.upper { text-transform: uppercase; }

then use the class as span class:

<span class="upper">
    <xsl:value-of select="myTextField" />
</span>

that's it :)

You can also use other transforms:

text-transform:  capitalize | uppercase | lowercase | none | inherit
Nida Sahar
  • 698
  • 4
  • 13
  • 29
  • 3
    I believe the OP was creating an Excel file; not HTML. – Daniel Haley Dec 01 '11 at 18:29
  • You can create Excel files by way of HTML. I believe it will honor CSS, but I'm not 100% sure. – iconoclast Aug 20 '13 at 14:45
  • 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. – Pekka Jan 06 '15 at 14:57
  • "I am using an XSLT stylesheet" is the key here. @iconoclast Yes you can (not a shameless plug, I swear): http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html – rainabba Nov 12 '15 at 23:52