home contribute faq download

FunctX XSLT Functions

fn:substring

A substring based on a starting point and optional length

Google
Webxsltfunctions.com

Description

The fn:substring function uses the $startingLoc argument to determine the starting location for the substring, where the first character is at position 1 (not 0). The optional $length argument indicates the number of characters to include, relative to the starting location. If no $length is provided, the entire rest of the string is included.

The function returns all characters whose position is greater than or equal to $startingLoc and less than ($startingLoc + $length). The $startingLoc number can be zero or negative, in which case the function starts at the beginning of the string, and still only include characters up to (but not including) the position at ($startingLoc + $length). If ($startingLoc + $length)is greater than the length of the string, the entire rest of the string is included.

This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book.

Arguments and Return Type

NameTypeDescription
$sourceString xs:string? the entire string
$startingLoc xs:double the starting character (1-based)
$length xs:double the number of characters to include
return value xs:string

Examples

XPath ExampleResults
substring('query', 1)
query
substring('query', 3)
ery
substring('query', 1, 1)
q
substring('query', 2, 3)
uer
substring('query', 2, 850)
uery
substring('query', 6, 2)
zero-length string
substring('query', -2)
query
substring('query', -2, 5)
qu
substring('query', 1, 0)
zero-length string
substring('', 1)
zero-length string
substring((), 1)
zero-length string

See Also

fn:substring-beforeThe substring before the first occurrence of a delimiter
fn:substring-afterThe substring after the first occurrence of a delimiter
functx:substring-before-lastThe substring before the last occurrence of a delimiter
functx:substring-after-lastThe substring after the last occurrence of a delimiter
functx:substring-after-last-matchThe substring after the last text that matches a regex
functx:substring-before-if-containsPerforms substring-before, returning the entire string if it does not contain the delimiter
functx:substring-after-if-containsPerforms substring-after, returning the entire string if it does not contain the delimiter

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XSLT Services

Recommended Reading:

XQuery