Quackit Logo
HTML
CSS
Scripting
Database
Hosting
Design
XML

XPath String Functions

XPath also includes a number of string functions.

String Functions

Here's a list of string functions you might find useful for your XPath expressions:

FunctionsDescription
starts-with(string1, string2)Returns true if the first string starts with the second string.
contains(string1, string2)Returns true if the first string contains the second string.
substring(stringoffsetlength)Returns a section of the string. The section starts at offset (which is a number), and is as long as the value provided at length (also a number).
substring-before(string1, string2)Returns the part of string1 up until the first occurence of string2.
substring-after(string1, string2)Returns the part of string1 after the first occurence of string2.
string-length(string)Returns the length of string (i.e. the number of characters).
normalize-space(string)Trims the leading and trailing space from string. Also replaces consecutive occurrences of white space with a single space.
translate(string1, string2, string3)Returns string1 after any matching characters in string2 have been replaced by the characters in string3.
concat(string1, string2, ...)Concatenates all strings (i.e. joins them together).
format-number(number1, string1, string2)Returns a formatted string version of number1 after applying string1 as a format string. string2 is an optional locale string.

Usage Example

The Source XML File

We'll use the following XML file, which contains a list of vegetables:

Code

The Requirement

Let's display the vegetable names in one column, and the string length of the vegetable name in another. Like this:

XPath string function example

The Solution

We could use the string-length() function in our XSL file to return the length of the string:

Code

Enjoy this page?

  1. Link to this page (copy/paste into your own website or blog):
  2. Link to Quackit using one of these banner ads.

Thanks for supporting Quackit!

Sponsored Link: Design CODE-FREE Websites