2009-12-09 76 views
3

我正在编写XSL转换,并且我的源代码中包含像这样的元素 - “title”。目标xml应该包含“标题”。有没有办法在XSL中使用字符串的第一个字母?在XSL中大写元素名称

+3

为什么这是一个社会维基? – 2009-12-09 10:54:49

+0

错误地检查了它。 – Anirudh 2009-12-09 11:07:37

回答

8

继从约翰尼斯说,创建使用XSL一个新的元素:元素你可能会做一些事情这样

<xsl:template match="*"> 
    <xsl:element name="{concat(upper-case(substring(name(), 1, 1)), substring(name(), 2))}"> 
     <xsl:value-of select="." /> 
    </xsl:element> 
</xsl:template> 

如果您正在使用XSLT1.0,你将无法使用大写功能。相反,你将有繁琐使-做翻译功能

<xsl:element name="{concat(translate(substring(name(), 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring(name(), 2))}"> 
+1

对于使用非拉丁字符的标签名称,“translate”将非常麻烦,尽管:-) – Joey 2009-12-09 11:12:33

1

我想你必须手动使用<xsl:element>然后像下面兽:

concat(upper-case(substring(name(), 1, 1)), substring(name(), 2)) 
+1

在XSLT 2.0之前''upper-case()'不可用。 – Tomalak 2009-12-09 18:00:33

+0

是的,Tim C已经注意到了。我没有意识到这一点,但无论如何你无法以一般情况解决它。除非你真的想把几个kibibytes放到'translate'中。 – Joey 2009-12-09 20:01:02

0

这是一个纯粹的XLST1模板,可以从ASCII句子创建CamelCase名称。

<xsl:template name="Capitalize"> 
    <xsl:param name="word" select="''"/> 
    <xsl:value-of select="concat(
     translate(substring($word, 1, 1), 
      'abcdefghijklmnopqrstuvwxyz', 
      'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 
     translate(substring($word, 2), 
      'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
      'abcdefghijklmnopqrstuvwxyz'))"/> 
</xsl:template> 
<xsl:template name="CamelCase-recursion"> 
    <xsl:param name="sentence" select="''"/> 
    <xsl:if test="$sentence != ''"> 
     <xsl:call-template name="Capitalize"> 
      <xsl:with-param name="word" select="substring-before(concat($sentence, ' '), ' ')"/> 
     </xsl:call-template> 
     <xsl:call-template name="CamelCase-recursion"> 
      <xsl:with-param name="sentence" select="substring-after($sentence, ' ')"/> 
     </xsl:call-template> 
    </xsl:if> 
</xsl:template> 
<xsl:template name="CamelCase"> 
    <xsl:param name="sentence" select="''"/> 
    <xsl:call-template name="CamelCase-recursion"> 
     <xsl:with-param name="sentence" select="normalize-space(translate($sentence, &quot;:;,'()_&quot;, ' '))"/> 
    </xsl:call-template> 
</xsl:template>