2012-03-08 119 views
14

我有以下xml。XSL - 如何将首字母大写

<Name> 
    <First>john</First> 
    <Last>smith</Last> 
</Name> 

我想大写第一个字母,并把它放在下面的合成文件中。

<FullName>John Smith</FullName> 

在此先感谢您。

+1

[functx:利用一(http://www.xsltfunctions.com/xsl/functx_capitalize-first.html) – 2012-03-08 01:18:39

回答

25

I. XSLT 2.0溶液

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*"> 
    <FullName><xsl:apply-templates/></FullName> 
</xsl:template> 

<xsl:template match="First|Last"> 
    <xsl:sequence select= 
    "concat(upper-case(substring(.,1,1)), 
      substring(., 2), 
      ' '[not(last())] 
     ) 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

当这个变换所提供的XML文档施加:

<Name> 
    <First>john</First> 
    <Last>smith</Last> 
</Name> 

有用,正确的结果产生

<FullName>John Smith</FullName> 

二, XSLT 1.0溶液

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:variable name="vLower" select= 
"'abcdefghijklmnopqrstuvwxyz'"/> 

<xsl:variable name="vUpper" select= 
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/> 

<xsl:template match="/*"> 
    <FullName><xsl:apply-templates/></FullName> 
</xsl:template> 

<xsl:template match="First|Last"> 
    <xsl:value-of select= 
    "concat(translate(substring(.,1,1), $vLower, $vUpper), 
      substring(., 2), 
      substring(' ', 1 div not(position()=last())) 
     ) 
    "/> 
</xsl:template> 
</xsl:stylesheet> 
0

尝试:

concat(
    translate(
    substring($Name, 1, 1), 
    'abcdefghijklmnopqrstuvwxyz', 
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
), 
    substring($Name,2,string-length($Name)-1) 
)