XSLT/xpath string functions

P

Peter Gerstbach

Hi,

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?

Peter
 
M

Martin Honnen

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:eek: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:param name="textToTransform" />
<xsl:variable name="head">
<xsl:choose>
<xsl:when test="contains($textToTransform, ' ')">
<xsl:value-of select="substring-before($textToTransform, ' ')" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="$textToTransform" />
</xsl:eek: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:eek:therwise>
<xsl:value-of select="$firstTransform" />
</xsl:eek: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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top