Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT/xpath string functions

Reply
Thread Tools

XSLT/xpath string functions

 
 
Peter Gerstbach
Guest
Posts: n/a
 
      08-13-2004
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-13-2004


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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput method="xml" encoding="UTF-8" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

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

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
 
Peter Gerstbach
Guest
Posts: n/a
 
      08-14-2004
Martin Honnen wrote:

>
> Here is my attempt with XSLT/XPath 1.0:
> ...


Thanks a lot, Martin! That rocks...

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the string functions (ex. find()) on a multi-symbol string korean_dave Python 2 06-17-2008 10:12 PM
searching an array, string compare functions, string sorting ckirchho@directmedia.de Javascript 2 10-10-2007 11:23 PM
String functions run on empty string Tim Slattery ASP General 1 07-11-2007 06:10 PM
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments