2012-03-08 18 views
14

Mam następujące xml.XSL - Jak wykorzystać pierwszą literę

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

Chcę, aby pierwsza litera została sformułowana wielkimi literami, a następnie sformatowana.

<FullName>John Smith</FullName> 

Z góry dziękuję.

+1

[functx: wykorzystać pierwszego] (http://www.xsltfunctions.com/xsl/functx_capitalize-first.html) –

Odpowiedz

25

I. XSLT 2.0 rozwiązanie:

<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> 

kiedy ta transformacja jest stosowane na dostarczonym dokumencie XML:

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

poszukiwany, poprawny wynik jest produkowany:

<FullName>John Smith</FullName> 

II. XSLT 1,0 roztwór:

<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

Spróbuj:

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