views:

354

answers:

2

I am writing a XSL transformation and my source has an element like this - "title". The target xml should contain "Title". Is there a way to capitalize the first letter of a string in XSL?

+1  A: 

I guess you have to manually use <xsl:element> and then something like the following beast:

concat(upper-case(substring(name(), 1, 1)), substring(name(), 2))
Joey
`upper-case()` is not available before XSLT 2.0.
Tomalak
Yes, Tim C noted that already. I wasn't aware of it but you can't solve it differently for the general case anyway. Unless you really want to put a few kibibytes into a `translate`.
Joey
+3  A: 

Following on from the Johannes said, to create a new element using xsl:element you would probably do something like this

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

If your are using XSLT1.0, you won't be able to use the upper-case function. Instead, you will have to make-do with the cumbersome translate function

    <xsl:element name="{concat(translate(substring(name(), 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring(name(), 2))}">
Tim C
`translate` will be very cumbersome for tag names using non-latin characters, though :-)
Joey