在xslt中将字符串的首字母转换为大写

编程入门 行业动态 更新时间:2024-10-24 09:31:06
本文介绍了在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中将字符串的首字母转换为大写

本文发布于:2023-10-16 05:54:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   中将   首字母   xslt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!