O'Reilly logo

XSLT Cookbook by Sal Mangano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the EXSLT String Extensions

Problem

You have good reason to use extension functions for string processing, but you are concerned about portability.

Solution

You may find that your XSLT processor already implements string functions defined by the EXSLT community (http://www.exslt.org/). At the time of publication, these functions are:

node-set str:tokenize(string input, string delimiters?)

The str:tokenize function splits up a string and returns a node set of token elements, each containing one token from the string.

The first argument is the string to be tokenized. The second argument is a string consisting of a number of characters. Each character in this string is taken as a delimiting character. The string given by the first argument is split at any occurrence of any character.

If the second argument is omitted, the default is the string 	

  (i.e., whitespace characters).

If the second argument is an empty string, the function returns a set of token elements, each of which holds a single character.

node-set str:replace(string, object search, object replace)

The str:replace function replaces any occurrences of search strings within a string with replacement nodes to create a node set.

The first argument gives the string within which strings are to be replaced.

The second argument is an object that specifies a search string list. If the second argument is a node set, then the search string list shows the result of converting each node in the node set to a string with the string() function, listed in document order. If the second argument is not a node set, then the second argument is converted to a string with the string() function, and the search string list consists of this string only.

The third argument is an object that specifies a replacement node list. If the third argument is a node set, then the replacement node list consists of the nodes in the node set in document order. If the third argument is not a node set, then the replacement node list consists of a single text node whose string value is the same as the result of converting the third argument to a string with the string() function.

string str:padding(number, string?)

The str:padding function creates a padding string of a certain length.

The first argument gives the length of the padding string to be created.

The second argument gives a string necessary to create the padding. This string is repeated as many times as is necessary to create a string of the length specified by the first argument; if the string is more than a character long, it may have to be truncated to produce the required length. If no second argument is specified, it defaults to a space (” “). If the second argument is an empty string, str:padding returns an empty string.

string str:align(string, string, string?)

The str:align function aligns a string within another string.

The first argument gives the target string to be aligned. The second argument gives the padding string within which it will be aligned.

If the target string is shorter than the padding string, then a range of characters in the padding string are replaced with those in the target string. Which characters are replaced depends on the value of the third argument, which gives the type of alignment. It can be left, right, or center. If no third argument is given or if it is not one of these values, then it defaults to left alignment.

With left alignment, the range of characters replaced by the target string begins with the first character in the padding string. With right alignment, the range of characters replaced by the target string ends with the last character in the padding string. With center alignment, the range of characters replaced by the target string is in the middle of the padding string so that either the number of unreplaced characters on either side of the range is the same or there is one less on the left than on the right.

If the target string is longer than the padding string, then it is truncated to be the same length as the padding string and returned.

string str:encode-uri(string)

The str:encode-uri function returns an encoded URI. The str:encode-uri method does not encode the following characters: “:”, "/“, ";“, and "?“.

A URI-encoded string converts unsafe and reserved characters with "%“, immediately followed by two hexadecimal digits (0-9, A-F) giving the ISO Latin 1 code for that character.

string str:decode-uri(string)

The str:decode-uri function decodes a string that has been URI-encoded. See str:encode-uri for an explanation.

string str:concat(node-set)

The str:concat function takes a node set and returns the concatenation of the string values of the nodes in that set. If the node set is empty, it returns an empty string.

node-set str:split(string, string?)

The str:split function splits up a string and returns a node set of token elements, each containing one token from the string. The first argument is the string to be split. The second is a pattern string. The string given by the first argument is split at any occurrence of this pattern.

If the second argument is omitted, the default is the string   (i.e., a space).

If the second argument is an empty string, the function returns a set of token elements, each of which holds a single character.

Discussion

Using the EXSLT string functions does not guarantee portability, since currently no XSLT implementation supports them all. In fact, according to the EXSLT web site, some functions have no current implementation. The EXSLT team makes up for this by providing native XSLT implementations, JavaScript, and/or MSXML implementations whenever possible.

A good reason for using EXSLT is that the members of the EXSLT team are very active in the XSLT community and many implementations will probably support most of their extensions eventually. It is also possible that some of their work will be incorporated into a future standard XSLT release.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required