XSLT/xpath string functions

Discussion in 'XML' started by Peter Gerstbach, Aug 13, 2004.

  1. 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
    Peter Gerstbach, Aug 13, 2004
    #1
    1. Advertising

  2. 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>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 13, 2004
    #2
    1. Advertising

  3. Martin Honnen wrote:

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


    Thanks a lot, Martin! That rocks... :)

    Peter
    Peter Gerstbach, Aug 14, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,958
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,370
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    509
  4. Son KwonNam
    Replies:
    4
    Views:
    6,992
    Joris Gillis
    Jan 5, 2005
  5. shaun
    Replies:
    1
    Views:
    14,741
    Dimitre Novatchev
    Nov 3, 2005
Loading...

Share This Page