已编辑:根据原始海报的说明,由下划线分隔的第一个字符串不应将其首字母大写。
此解决方案不使用递归,应该更有效。
这是新的解决方案:
string-join(
(for $i in 1 to count(tokenize(.,'_')),
$s in tokenize(.,'_')[$i],
$fl in substring($s,1,1),
$tail in substring($s,2)
return
if($i eq 1)
then $s
else concat(upper-case($fl), $tail)
),
''
)
结果现在完全符合要求:
underLinedString
下面是旧的解决方案。
replace() 函数假设你有一个固定的替换——因此它不是解决这个问题的最佳工具。
这是一个单行 XPath 2.0 解决方案(它当然可以用作 XSLT 2.0 转换的一部分:)):
string-join(
(for $s in tokenize(.,'_'),
$fl in substring($s,1,1),
$tail in substring($s,2)
return
concat(upper-case($fl), $tail)
),
''
)
当我们在这样的 XSLT 2.0 转换中使用上述表达式时:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:sequence select=
"string-join(
(for $s in tokenize(.,'_'),
$fl in substring($s,1,1),
$tail in substring($s,2)
return
concat(upper-case($fl), $tail)
),
''
)
"/>
</xsl:template>
</xsl:stylesheet>
并将其应用于此 XML 文档:
<t>under_lined_String</t>
the wanted result is produced:
UnderLinedString