string 如何使用 XSLT 将字符串转换为大写或小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/586231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert a string to upper- or lower-case with XSLT?
提问by mjs
How do you do case conversion in XSL?
你如何在 XSL 中进行大小写转换?
<xsl:variable name="upper">UPPER CASE</xsl:variable>
<xsl:variable name="lower" select="???"/>
回答by Jon W
In XSLT 1.0 the upper-case()
and lower-case()
functions are not available.
If you're using a 1.0 stylesheet the common method of case conversion is translate()
:
在 XSLT 1.0 中,upper-case()
和lower-case()
函数不可用。如果您使用的是 1.0 样式表,则大小写转换的常用方法是translate()
:
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:template match="/">
<xsl:value-of select="translate(doc, $lowercase, $uppercase)" />
</xsl:template>
回答by Anton Gogolev
XSLT 2.0 has upper-case()
and lower-case()
functions. In case of XSLT 1.0, you can use translate()
:
XSLT 2.0 具有upper-case()
和lower-case()
功能。在 XSLT 1.0 的情况下,您可以使用translate()
:
<xsl:value-of select="translate("xslt", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")" />
回答by Vladislav
.NET XSLT implementation allows to write custom managed functions in the stylesheet. For lower-case() it can be:
.NET XSLT 实现允许在样式表中编写自定义托管函数。对于小写(),它可以是:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:utils="urn:myExtension" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<msxsl:script implements-prefix="utils" language="C#">
<![CDATA[
public string ToLower(string stringValue)
{
string result = String.Empty;
if(!String.IsNullOrEmpty(stringValue))
{
result = stringValue.ToLower();
}
return result;
}
]]>
</msxsl:script>
<!-- using of our custom function -->
<lowercaseValue>
<xsl:value-of select="utils:ToLower($myParam)"/>
</lowercaseValue>
Assume, that can be slow, but still acceptable.
假设,这可能很慢,但仍然可以接受。
Do not forget to enable embedded scripts support for transform:
不要忘记为转换启用嵌入式脚本支持:
// Create the XsltSettings object with script enabled.
XsltSettings xsltSettings = new XsltSettings(false, true);
XslCompiledTransform xslt = new XslCompiledTransform();
// Load stylesheet
xslt.Load(xsltPath, xsltSettings, new XmlUrlResolver());
回答by Natalie Pan
<xsl:variable name="upper">UPPER CASE</xsl:variable>
<xsl:variable name="lower" select="translate($upper,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
<xsl:value-of select ="$lower"/>
//displays UPPER CASE as upper case
回答by Oleksii Kyslytsyn
For ANSI character encoding:
对于 ANSI 字符编码:
translate(//variable, 'ABCDEFGHIJKLMNOPQRSTUVWXYZàá??????èéê?ìí??D?òó????ùú?üYT????', 'abcdefghijklmnopqrstuvwxyzàáa?????èéê?ìí??e?òó????ùú?üyt????')
回答by Eric Petroelje
upper-case(string) and lower-case(string)
大写(字符串)和小写(字符串)