XSL - 如何将首字母大写

AJP*_*AJP 17 xml xslt

我有以下xml.

<Name>
  <First>john</First>
  <Last>smith</Last>
</Name>
Run Code Online (Sandbox Code Playgroud)

我想把首字母大写,然后输入以下格式.

 <FullName>John Smith</FullName>
Run Code Online (Sandbox Code Playgroud)

先感谢您.

Dim*_*hev 30

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>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于提供的XML文档时:

<Name>
    <First>john</First>
    <Last>smith</Last>
</Name>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

<FullName>John Smith</FullName>
Run Code Online (Sandbox Code Playgroud)

II.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>
Run Code Online (Sandbox Code Playgroud)