String Processing

Steve Ball

$Id: string.html,v 1.11 2004/10/10 06:18:57 balls Exp $


Introduction

This module provides templates for manipulating strings.

Table of Contents

str:to-upper - Make string uppercase
str:to-lower - Make string lowercase
str:capitalise - Capitalise string
str:to-camelcase - Convert a string to one camelcase word
str:substring-before-first - String extraction
str:substring-after-last - String extraction
str:substring-before-last - String extraction
str:subst - String substitution
str:count-substring - Count Substrings
str:substring-after-at - String extraction
str:substring-before-at - String extraction
str:insert-at - String insertion
str:backward - String reversal
str:justify - Format a string
str:character-first - Find first occurring character in a string
str:string-match - Match A String To A Pattern
str:generate-string - Create A Repeating Sequence of Characters

Name

str:to-upper — Make string uppercase

Synopsis

<xsl:template name="str:to-upper"><xsl:param name="text"/><xsl:param name="modified-text">
      <xsl:call-template name="str:subst">
        <xsl:with-param name="text">
          <xsl:value-of select="$text"/>
        </xsl:with-param>
        <xsl:with-param name="replace">
          <xsl:text>ß</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="with">
          <xsl:text>S</xsl:text>
          <xsl:text>S</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:param>  ...</xsl:template>

Converts all lowercase letters to uppercase.

Parameters
text

The string to be converted

Returns

Returns string with all uppercase letters.



Name

str:to-lower — Make string lowercase

Synopsis

<xsl:template name="str:to-lower"><xsl:param name="text"/>  ...</xsl:template>

Converts all uppercase letters to lowercase.

Parameters
text

The string to be converted

Returns

Returns string with all lowercase letters.



Name

str:capitalise — Capitalise string

Synopsis

<xsl:template name="str:capitalise"><xsl:param name="text"/><xsl:param name="all" select="true()"/>  ...</xsl:template>

Converts first character of string to an uppercase letter. All remaining characters are converted to lowercase.

Parameters
text

The string to be capitalised

all

Boolean controlling whether all words in the string are capitalised.

Default is true.

Returns

Returns string with first character uppcase and all remaining characters lowercase.



Name

str:to-camelcase — Convert a string to one camelcase word

Synopsis

<xsl:template name="str:to-camelcase"><xsl:param name="text"/><xsl:param name="upper" select="true()"/><xsl:param name="string-with-only-spaces">
      <xsl:value-of select="translate($text,concat($xsltsl-str-ws,'/'),'     ')"/>
    </xsl:param><xsl:param name="before-space-removal">
      <xsl:variable name="firstword">
        <xsl:call-template name="str:substring-before-first">
          <xsl:with-param name="text" select="$string-with-only-spaces"/>
          <xsl:with-param name="chars" select="$xsltsl-str-ws"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="$upper">
          <xsl:call-template name="str:to-upper">
            <xsl:with-param name="text" select="substring($firstword, 1, 1)"/>
          </xsl:call-template>
          <xsl:call-template name="str:to-lower">
            <xsl:with-param name="text" select="substring($firstword, 2)"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="str:to-lower">
            <xsl:with-param name="text" select="$firstword"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:call-template name="str:capitalise">
        <xsl:with-param name="text">
          <xsl:value-of select="substring($string-with-only-spaces, string-length($firstword) + 2)"/>
        </xsl:with-param>
        <xsl:with-param name="all" select="true()"/>            
      </xsl:call-template>
    </xsl:param>  ...</xsl:template>

Converts a string to one lowerCamelCase or UpperCamelCase word, depending on the setting of the "upper" parameter. UpperCamelCase is also called MixedCase while lowerCamelCase is also called just camelCase. The template removes any spaces, tabs and slashes, but doesn't deal with other punctuation. It's purpose is to convert strings like "hollow timber flush door" to a term suitable as identifier or XML tag like "HollowTimberFlushDoor".

Parameters
text

The string to be capitalised

upper

Boolean controlling whether the string becomes an UpperCamelCase word or a lowerCamelCase word.

Default is true.

Returns

Returns string with first character uppcase and all remaining characters lowercase.



Name

str:substring-before-first — String extraction

Synopsis

<xsl:template name="str:substring-before-first"><xsl:param name="text"/><xsl:param name="chars"/>  ...</xsl:template>

Extracts the portion of string 'text' which occurs before any of the characters in string 'chars'.

Parameters
text

The string from which to extract a substring.

chars

The string containing characters to find.

Returns

Returns string.



Name

str:substring-after-last — String extraction

Synopsis

<xsl:template name="str:substring-after-last"><xsl:param name="text"/><xsl:param name="chars"/>  ...</xsl:template>

Extracts the portion of string 'text' which occurs after the last of the character in string 'chars'.

Parameters
text

The string from which to extract a substring.

chars

The string containing characters to find.

Returns

Returns string.



Name

str:substring-before-last — String extraction

Synopsis

<xsl:template name="str:substring-before-last"><xsl:param name="text"/><xsl:param name="chars"/>  ...</xsl:template>

Extracts the portion of string 'text' which occurs before the first character of the last occurance of string 'chars'.

Parameters
text

The string from which to extract a substring.

chars

The string containing characters to find.

Returns

Returns string.



Name

str:subst — String substitution

Synopsis

<xsl:template name="str:subst"><xsl:param name="text"/><xsl:param name="replace"/><xsl:param name="with"/><xsl:param name="disable-output-escaping">no</xsl:param>  ...</xsl:template>

Substitute 'replace' for 'with' in string 'text'.

Parameters
text

The string upon which to perform substitution.

replace

The string to substitute.

with

The string to be substituted.

disable-output-escaping

A value of yes indicates that the result should have output escaping disabled. Any other value allows normal escaping of text values. The default is to enable output escaping.

Returns

Returns string.



Name

str:count-substring — Count Substrings

Synopsis

<xsl:template name="str:count-substring"><xsl:param name="text"/><xsl:param name="chars"/>  ...</xsl:template>

Counts the number of times a substring occurs in a string. This can also counts the number of times a character occurs in a string, since a character is simply a string of length 1.

Example 1. Counting Lines

<xsl:call-template name="str:count-substring">
  <xsl:with-param name="text" select="$mytext"/>
  <xsl:with-param name="chars" select="'&#x0a;'"/>
</xsl:call-template>
Parameters
text

The source string.

chars

The substring to count.

Returns

Returns a non-negative integer value.



Name

str:substring-after-at — String extraction

Synopsis

<xsl:template name="str:substring-after-at"><xsl:param name="text"/><xsl:param name="chars"/><xsl:param name="position"/><xsl:param name="all" select="false()"/>  ...</xsl:template>

Extracts the portion of a 'char' delimited 'text' string "array" at a given 'position'.

Parameters
text

The string from which to extract a substring.

chars

delimiters

position

position of the elements

all

If true all of the remaining string is returned, otherwise only the element at the given position is returned. Default: false().

Returns

Returns string.



Name

str:substring-before-at — String extraction

Synopsis

<xsl:template name="str:substring-before-at"><xsl:param name="text"/><xsl:param name="chars"/><xsl:param name="position"/>  ...</xsl:template>

Extracts the portion of a 'char' delimited 'text' string "array" at a given 'position'

Parameters
text

The string from which to extract a substring.

chars

delimiters

position

position of the elements

Returns

Returns string.



Name

str:insert-at — String insertion

Synopsis

<xsl:template name="str:insert-at"><xsl:param name="text"/><xsl:param name="position"/><xsl:param name="chars"/>  ...</xsl:template>

Insert 'chars' into "text' at any given "position'

Parameters
text

The string upon which to perform insertion

position

the position where insertion will be performed

with

The string to be inserted

Returns

Returns string.



Name

str:backward — String reversal

Synopsis

<xsl:template name="str:backward"><xsl:param name="text"/>  ...</xsl:template>

Reverse the content of a given string

Parameters
text

The string to be reversed

Returns

Returns string.



Name

str:justify — Format a string

Synopsis

<xsl:template name="str:justify"><xsl:param name="text"/><xsl:param name="max" select=""80""/><xsl:param name="indent" select=""0""/><xsl:param name="justify" select=""left""/>  ...</xsl:template>

Inserts newlines and spaces into a string to format it as a block of text.

Parameters
text

String to be formatted.

max

Maximum line length.

indent

Number of spaces to insert at the beginning of each line.

justify

Justify left, right or both. Not currently implemented (fixed at "left").

Returns

Formatted block of text.



Name

str:character-first — Find first occurring character in a string

Synopsis

<xsl:template name="str:character-first"><xsl:param name="text"/><xsl:param name="chars"/>  ...</xsl:template>

Finds which of the given characters occurs first in a string.

Parameters
text

The source string.

chars

The characters to search for.



Name

str:string-match — Match A String To A Pattern

Synopsis

<xsl:template name="str:string-match"><xsl:param name="text"/><xsl:param name="pattern"/>  ...</xsl:template>

Performs globbing-style pattern matching on a string.

Example 2. Match Pattern

<xsl:call-template name="str:string-match">
  <xsl:with-param name="text" select="$mytext"/>
  <xsl:with-param name="pattern" select="'abc*def?g'"/>
</xsl:call-template>
Parameters
text

The source string.

pattern

The pattern to match against. Certain characters have special meaning:

*

Matches zero or more characters.

?

Matches a single character.

\

Character escape. The next character is taken as a literal character.

Returns

Returns "1" if the string matches the pattern, "0" otherwise.



Name

str:generate-string — Create A Repeating Sequence of Characters

Synopsis

<xsl:template name="str:generate-string"><xsl:param name="text"/><xsl:param name="count"/>  ...</xsl:template>

Repeats a string a given number of times.

Parameters
text

The string to repeat.

count

The number of times to repeat the string.