DEV Community

Alexandre Vazquez
Alexandre Vazquez

Posted on • Originally published at blog.xsltplayground.com

XSLT string functions: complete reference with examples

String manipulation is one of the most common tasks in XSLT. Whether you are formatting output, parsing codes, or normalising values from external systems, XPath provides a rich set of string functions. This reference covers the most useful ones with examples you can run in XSLT Playground.

Basic string functions (XSLT 1.0+)

string-length

Returns the number of characters in a string.



Enter fullscreen mode Exit fullscreen mode

substring

Extracts a portion of a string. Arguments: string, start position (1-based), optional length.



Enter fullscreen mode Exit fullscreen mode

substring-before and substring-after

Split a string on a delimiter.



Enter fullscreen mode Exit fullscreen mode

contains, starts-with, ends-with

Test membership without extracting.

...
...

...
Enter fullscreen mode Exit fullscreen mode

concat

Joins strings together. Takes any number of arguments.



Enter fullscreen mode Exit fullscreen mode

normalize-space

Strips leading and trailing whitespace, and collapses internal whitespace to single spaces. Essential for cleaning values from XML sources.


Enter fullscreen mode Exit fullscreen mode

translate

Replaces characters one-for-one. Useful for simple case conversion or character removal.






Enter fullscreen mode Exit fullscreen mode

Advanced string functions (XSLT 2.0+)

upper-case and lower-case

No longer need translate for case conversion.



Enter fullscreen mode Exit fullscreen mode

replace

Regex-based substitution. Much more powerful than translate.









Enter fullscreen mode Exit fullscreen mode

matches

Tests a string against a regex.




Enter fullscreen mode Exit fullscreen mode

tokenize

Splits a string into a sequence using a regex delimiter. Returns a sequence of strings.









Enter fullscreen mode Exit fullscreen mode

string-join

The inverse of tokenize. Joins a sequence with a separator.



Enter fullscreen mode Exit fullscreen mode

format-number

Formats a number as a string with a picture pattern.





Enter fullscreen mode Exit fullscreen mode

format-date and format-dateTime

Format xs:date and xs:dateTime values using picture strings.






Enter fullscreen mode Exit fullscreen mode

Practical patterns

Extract domain from URL:



Enter fullscreen mode Exit fullscreen mode

Pad a number with leading zeros:


Enter fullscreen mode Exit fullscreen mode

Check if a node text is numeric:


Enter fullscreen mode Exit fullscreen mode

All of these work in XSLT Playground. Set the version to 2.0 or 3.0 for the functions that require it.

Top comments (0)