2009-12-09 54 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>