2012-03-08 22 views
14

Ho seguito xml.XSL - Come capitalizzare la prima lettera

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

Voglio mettere in maiuscolo la prima lettera e mettere il seguente modulo.

<FullName>John Smith</FullName> 

Grazie in anticipo. soluzione

+1

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

risposta

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> 

quando tale trasformazione viene applicato sul documento XML fornito:

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

The Wanted, risultato corretto è prodotto:

<FullName>John Smith</FullName> 

II. XSLT 1.0 soluzione:

<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

Prova:

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