Use item name in XSL

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

+3
source share
4 answers

After Johannes said to create a new element using xsl: element , you will 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 you use XSLT1.0, you cannot use the uppercase function . Instead, you have to do something with a cumbersome translate function

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

I think you need to manually use <xsl:element>, and then something like the following beast:

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

Here is a clean XLST1 template that creates CamelCase names from ASCII clauses.

<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>
0
source

Source: https://habr.com/ru/post/1725147/


All Articles