Peter Gerstbach wrote:
I want to convert with XSLT/XPATH a String like "Aaa bbb ccc" with
variant length into to "AaaBbbCcc".
I think it should be possible with these steps:
1) tokenize the String with ' ' as separator with tokenize()
2) make the first character uppercase with substring() and upper-case()
3) put them together with concat()
Can anybody tell me, how I glue those steps together in XSLT?
Here is my attempt with XSLT/XPath 1.0:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl
utput method="xml" encoding="UTF-8" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template name="upperCase">
<xsl
aram name="textToTransform" />
<xsl:variable name="head">
<xsl:choose>
<xsl:when test="contains($textToTransform, ' ')">
<xsl:value-of select="substring-before($textToTransform, ' ')" />
</xsl:when>
<xsl
therwise>
<xsl:value-of select="$textToTransform" />
</xsl
therwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="tail" select="substring-after($textToTransform, '
')" />
<xsl:variable name="firstTransform"
select="concat(translate(substring($head, 1, 1),
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
substring($head, 2))" />
<xsl:choose>
<xsl:when test="$tail">
<xsl:value-of select="$firstTransform" />
<xsl:call-template name="upperCase">
<xsl:with-param name="textToTransform" select="$tail" />
</xsl:call-template>
</xsl:when>
<xsl
therwise>
<xsl:value-of select="$firstTransform" />
</xsl
therwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text/text()">
<xsl:call-template name="upperCase">
<xsl:with-param name="textToTransform" select="normalize-space(.)" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Test document
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text>Aaa bbb ccc</text>
<text>xxx yyy zzzzz </text>
</root>
is transformed to
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text>AaaBbbCcc</text>
<text>XxxYyyZzzzz</text>
</root>