XML technologies / XSLT / XSLT Cookbook / Strings / Using EXSLT string extensions
Using EXSLT string extensions
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You have good reasons to use string extension functions but worry about portability.
Solution
Your XSLT processor may already implement string functions defined by the EXSLT community. At publication, these were:
node-set str:tokenize(string input, string delimiters?)
Function str:tokenize splits a string into a node set of token elements, each containing one token. The first argument is the string; the second contains delimiter characters. The input is split at every occurrence of any such character. Without the second argument, its default is 	

  (i.e. whitespace characters). An empty second argument returns token elements each containing one character.
node-set str:replace(string, object search, object replace)
Function str:replace replaces every search-string occurrence with replacement nodes, producing a node set. The first argument is the input string. The second defines a search-string list. If it is a node set, convert each node to a string in document order with string( ). Otherwise, convert the argument with string( ) to obtain a single search string. The third argument defines the replacement-node list. For a node set, these are its nodes in document order. Otherwise, the list contains a single text node whose value is the third argument converted by string( ).
string str:padding(number, string?)
Function str:padding creates padding of a specified length. The first argument gives the length; the second is repeated as often as necessary to fill it. A multicharacter string may be truncated to fit. Without the second argument, a space (" ") is used. If it is empty, str:padding returns an empty string.
string str:align(string, string, string?)
Function str:align aligns a string inside another. The first argument is the target string and the second the padding. If the target is shorter, its characters replace a region of the padding. The third argument determines which region through alignment, either left, right or center. If absent or invalid, left alignment applies. With left alignment, replacement starts at the padding’s first character; with right alignment, it ends at its last. Centering leaves equal numbers of unreplaced characters on both sides, or one fewer on the left. If the target is longer than the padding, it is truncated to the same length and returned.
string str:encode-uri(string)
Function str:encode-uri returns an encoded URI. Method str:encode-uri does not encode these characters: “:”, “/”, “;” and “?”. URI encoding represents unsafe and reserved characters with “%” immediately followed by two hexadecimal digits (0–9, A-F) giving the character’s ISO-Latin-1 code.
string str:decode-uri(string)
Function str:decode-uri decodes a URI-encoded string. See the explanation under str:encode-uri.
string str:concat(node-set)
Function str:concat takes a node set and returns the concatenation of its nodes’ string values, or the empty string for an empty set.
node-set str:split(string, string?)
Function str:split splits a string into token elements. The first argument is the input and the second a pattern string. Every occurrence of the pattern splits the input. Without the second argument, its default is   (a space). An empty second argument returns token elements each containing one character.
Discussion
EXSLT string functions do not guarantee portability: currently no implementation supports all of them, and according to the website some have no implementation at all. Where possible, the team compensates with native XSLT, JavaScript and/or MSXML implementations.
The EXSLT team is very active in the XSLT community, so many implementations will probably eventually support most extensions. Some of its work may also enter future standard XSLT versions. This is a good reason to use EXSLT.
See also
In XSLT 1.0 kurz & gut (O’Reilly), the EXSLT reference includes notes comparing many XPath/XSLT 2.0 functions performing similar or identical tasks.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de