XSLT/xpath string functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Gerstbach

    #1

    XSLT/xpath string functions

    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
  • Martin Honnen

    #2
    Re: XSLT/xpath string functions



    Peter Gerstbach wrote:

    [color=blue]
    > 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?[/color]

    Here is my attempt with XSLT/XPath 1.0:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:styleshe et
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output 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="textToTra nsform" />
    <xsl:variable name="head">
    <xsl:choose>
    <xsl:when test="contains( $textToTransfor m, ' ')">
    <xsl:value-of select="substri ng-before($textToT ransform, ' ')" />
    </xsl:when>
    <xsl:otherwis e>
    <xsl:value-of select="$textTo Transform" />
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:variable name="tail" select="substri ng-after($textToTr ansform, '
    ')" />
    <xsl:variable name="firstTran sform"
    select="concat( translate(subst ring($head, 1, 1),
    'abcdefghijklmn opqrstuvwxyz', 'ABCDEFGHIJKLMN OPQRSTUVWXYZ'),
    substring($head , 2))" />
    <xsl:choose>
    <xsl:when test="$tail">
    <xsl:value-of select="$firstT ransform" />
    <xsl:call-template name="upperCase ">
    <xsl:with-param name="textToTra nsform" select="$tail" />
    </xsl:call-template>
    </xsl:when>
    <xsl:otherwis e>
    <xsl:value-of select="$firstT ransform" />
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>

    <xsl:template match="text/text()">
    <xsl:call-template name="upperCase ">
    <xsl:with-param name="textToTra nsform" select="normali ze-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>XxxYyyZzz zz</text>
    </root>
    --

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

    Comment

    • Peter Gerstbach

      #3
      Re: XSLT/xpath string functions

      Martin Honnen wrote:
      [color=blue]
      >
      > Here is my attempt with XSLT/XPath 1.0:
      > ...[/color]

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

      Peter

      Comment

      Working...