将每个单词的第一个字符转换为大写 [英] Convert First character of each word to upper case

查看:36
本文介绍了将每个单词的第一个字符转换为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我需要使用 xsl 将每个单词的第一个字母转换为大写,其余为小写,例如,

I have a String and I need to convert the first letter of each word to upper case and rest to lower case using xsl, For example,

输入字符串= dInEsh sAchdeV kApil Muk

Input String= dInEsh sAchdeV kApil Muk

所需的输出字符串= Dinesh Sachdev Kapil Muk

Desired Output String= Dinesh Sachdev Kapil Muk

虽然,我知道我必须为此目的使用翻译功能,但是如何使用 XSLT 1.0 将每个单词的第一个宪章翻译成大写,并全部转为小写

Although, I know I have to use translate function for the purpose but how can I translate the first charter of each word to Upper-case and rest all in lower- case using XSLT 1.0

谢谢

推荐答案

以下内容并不好",我相信有人(主要是 Dimitri)可以想出更简单的方法(尤其是在 XSLT 2.0 中).. 但我已经测试了这个并且它有效

The following is not "nice", and I'm sure somebody (mainly Dimitri) could come up with something far simpler (especially in XSLT 2.0)... but I've tested this and it works

<xsl:template name="CamelCase">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="contains($text,' ')">
      <xsl:call-template name="CamelCaseWord">
        <xsl:with-param name="text" select="substring-before($text,' ')"/>
      </xsl:call-template>
      <xsl:text> </xsl:text>
      <xsl:call-template name="CamelCase">
        <xsl:with-param name="text" select="substring-after($text,' ')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="CamelCaseWord">
        <xsl:with-param name="text" select="$text"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="CamelCaseWord">
  <xsl:param name="text"/>
  <xsl:value-of select="translate(substring($text,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /><xsl:value-of select="translate(substring($text,2,string-length($text)-1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:template>

基本思想是你调用CamelCase,如果它找到一个空格,那么它在空格之前的所有上运行CamelCaseWord(即第一个单词),然后再次调用 CamelCaseafter 空格后的所有内容(即句子的其余部分).否则,如果找不到空格(因为它已经到了句子中的最后一个单词),那么它只会调用 CamelCaseWord.

The basic idea is that you call CamelCase, if it find a space, then it runs CamelCaseWord on everything before the space (i.e. the first word) and then calls CamelCase again with the everything after the space (i.e. the rest of the sentence). Otherwise if no space is found (because it's got to the last word in the sentence), then it just calls CamelCaseWord.

CamelCaseWord 模板只是将第一个字符从低到高(如果需要)和所有剩余的字符从高到低(如果需要)转换.

The CamelCaseWord template simply translates the first character from lower to upper (if necessary) and all remaining characters from upper to lower (if necessary).

因此,您可以将其称为...

So to call it you'd have...

<xsl:call-template name="CamelCase">
   <xsl:with-param name="text">dInEsh sAchdeV kApil Muk</xsl:with-param>
</xsl:call-template>

这篇关于将每个单词的第一个字符转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆