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.
Related videos on Youtube
Author by
Admin
Updated on April 29, 2020Comments
-
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 over 14 yearsAsker's name looks vaguely French... what happens to é ? (sorry, I couldn't resist...)
-
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 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 over 12 yearsI believe the OP was creating an Excel file; not HTML.
-
Azhar Khorasany over 11 yearsAbsolutely beautiful and easy :)
-
iconoclast over 10 yearsYou can create Excel files by way of HTML. I believe it will honor CSS, but I'm not 100% sure.
-
Pekka over 9 yearsThe 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 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/…