#String utility functions (yes, functions)
version: 1.03
author: Simon de Turck
email: simon@zimmen.com
This utility:
- is created for use in Symphony-CMS. The XSLT-powered open source content management system.
- uses some EXSLT that is supported in libxslt 1.0.19 and later
##Usage:
To use these functions, add the string namespace to your master (or wherever you feel it suits best) stylesheet and import this utility.
Add the namespace xmlns:string="http://symphony-cms.com/functions"
to all stylesheets where you want to use these functions as well.
##Convert string to lowercase: <xsl:value-of select="string:lower-case([selector])" />
###example: <xsl:value-of select="string:lower-case('ABC')" />
abc
##Convert string to uppercase: <xsl:value-of select="string:upper-case([selector])" />
###example: <xsl:value-of select="string:upper-case('abc')" />
ABC
##Capitalize string: <xsl:value-of select="capitalize([selector])" /> <xsl:value-of select="ucfirst([selector])" /> (alias)
###example: <xsl:value-of select="capitalize('the t in this sentence is capitalized!')" />
The t in this sentence is capitalized!
##Capitalize each word in a string: <xsl:value-of select="capitalize-words([selector])" /> <xsl:value-of select="ucall([selector])" /> (alias)
###example: <xsl:value-of select="capitalize-words('all words in this sentence are capitalized!')" />
All Words In This Sentence Are Capitalized !
##Replace in string: <xsl:value-of select="string:replace([selector],[needle],[replace])" />
###example: <xsl:value-of select="string:replace('a b c',' ',' to the ')" />
a to the b to the c
##Split a string (returns a nodeset): <xsl:copy-of select="string:split([selector][,[delimiter],[rootnodename],[nodename]])" />
- Default delimiter: ','
- Default rootnodename: 'nodeset'
- Default nodename: 'node'
###example 1: <xsl:copy-of select="string:split('a,b,c')" />
<nodeset>
<node>a</node>
<node>b</node>
<node>c</node>
</nodeset>
###example 2: <xsl:copy-of select="string:split('book1#book2#book3','#','bookstore','book')" />
<bookstore>
<book>book1</book>
<book>book2</book>
<book>book3</book>
</bookstore>
###example 3: <xsl:copy-of select="string:split('item1#item2#item3','#','ul','li')" />
<ul>
<li>book1</li>
<li>book2</li>
<li>book3</li>
</ul>
##Count occurrences of a string in a string:
To count elements (lets say <a href>
's in your text fields) you should use count(text/*/a)
instead.
<xsl:value-of select="string:substring-count([needle],[haystack])" />
###example <xsl:value-of select="string:substring-count('the quick brown fox jumps over the lazy dog','the')" />
2