2012-03-08 10 views
14

Tôi đã theo dõi xml.XSL - Làm cách nào để viết hoa chữ cái đầu tiên

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

Tôi muốn viết hoa chữ cái đầu tiên và đặt trong định dạng sau.

<FullName>John Smith</FullName> 

Cảm ơn bạn trước. giải pháp

+1

[functx: viết hoa đầu tiên] (http://www.xsltfunctions.com/xsl/functx_capitalize-first.html) –

Trả lời

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> 

khi chuyển đổi này được áp dụng trên các tài liệu được cung cấp XML:

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

các truy nã, kết quả chính xác được sản xuất:

<FullName>John Smith</FullName> 

II. XSLT giải pháp 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

Hãy thử:

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