XSLT Stylesheet: Changing text to upper case

喜你入骨 提交于 2019-11-28 20:22:51
David Christiansen

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>

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.

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

SparrowEatsHawk

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.

MojoJojo

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!