在xslt中将字符串的首字母转换为大写 [英] Converting first letter of a string to capital in xslt

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

问题描述

我是XSLT的新手,我需要一些帮助. 我将数据存储在XML文件中,并且有一个属性:

I am new to XSLT and I need some help. I have data stored in XML file and there is one attribute :

<clientName>JOHN GEORGE SMITH</clientName> 

我正在HTML文件中打印此名称,如下所示:

I am printing this name in my HTML file as follows:

<div>
    <xsl:value-of select="clientName"/>
</div>

我希望输出为: John George Smith .

我希望每个单词的首字母大写.我尝试在线查找解决方案,但找不到合适的方法.

I want the first letter to be capital for each word. I tried finding the solution online but couldn't find an appropriate way.

谢谢.

推荐答案

这里的真正问题不是如何大写每个单词,而是如何将给定文本标记为单个单词.

The real problem here is not how to capitalize each word, but how to tokenize the given text to individual words.

如果可以假设单词始终由空格分隔,或者至少由以空格结尾的字符串分隔,那么您可以按照以下方式进行操作:

If it can be assumed that words are always separated by a space - or at least by a string that ends with a space - then you could do it this way:

<xsl:template match="something">

    <!-- some stuff -->

    <div>
        <xsl:call-template name="capitalize">
            <xsl:with-param name="text" select="clientName"/>
        </xsl:call-template>
    </div>

    <!-- other stuff -->

</xsl:template>


<xsl:template name="capitalize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="' '"/>

    <xsl:variable name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:variable name="lower-case" select="'abcdefghijklmnopqrstuvwxyz'"/>

    <xsl:variable name="word" select="substring-before(concat($text, $delimiter), $delimiter)" />
    <xsl:value-of select="translate(substring($word, 1, 1), $lower-case, $upper-case)"/>    
    <xsl:value-of select="translate(substring($word, 2), $upper-case, $lower-case)"/>
    <xsl:if test="contains($text, $delimiter)">
        <xsl:value-of select="$delimiter"/>
        <!-- recursive call -->
        <xsl:call-template name="capitalize">
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>


请注意,这将失败,例如:


Note that this will fail with values such as:

<clientName>HILLARY RODHAM-CLINTON</clientName>
<clientName>HILLARY (RODHAM) CLINTON</clientName>
<clientName>GEORGE BUSH THE THIRD</clientName>
<clientName>CHARLES DE GAULLE</clientName>
<clientName>RENÉE ZELLWEGER</clientName>

可能还有我目前无法想到的其他人.

and probably others I cannot think of at the moment.

这篇关于在xslt中将字符串的首字母转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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