home contribute faq download

FunctX XQuery Functions

functx:substring-before-last

The substring before the last occurrence of a delimiter

Google
Webxqueryfunctions.com

Description

The functx:substring-before-last function returns the part of $arg that appears before the last occurrence of $delim. If $arg does not contain $delim, a zero-length string is returned.

Arguments and Return Type

NameTypeDescription
$arg xs:string? the string to substring
$delim xs:string the delimiter
return value xs:string

XQuery Function Declaration

See XSLT definition.
XQuery Syntax for July 2004 - January 2007 (1.0):
declare namespace functx = "http://www.functx.com"; 
declare function functx:substring-before-last 
  ( $arg as xs:string? ,
    $delim as xs:string )  as xs:string {
       
   if (matches($arg, functx:escape-for-regex($delim)))
   then replace($arg,
            concat('^(.*)', functx:escape-for-regex($delim),'.*'),
            '$1')
   else ''
 } ;
XQuery Syntax for May 2003:
declare namespace functx = "http://www.functx.com" 
define function substring-before-last 
  ( $arg as xs:string? ,
    $delim as xs:string )  as xs:string {
       
   if (fn:matches($arg, functx:escape-for-regex($delim)))
   then fn:replace($arg,
            fn:concat('^(.*)', functx:escape-for-regex($delim),'.*'),
            '$1')
   else ''
 }

Examples

XQuery ExampleResults
functx:substring-before-last(
   'abc-def-ghi', '-')
abc-def
functx:substring-before-last('abcd-abcd', 'ab')
abcd-
functx:substring-before-last('abcd-abcd', 'x')
zero-length string

Depends On

functx:escape-for-regexEscapes regex special characters

See Also

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

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Need XQuery Help?
D A T Y P I C
Training | Consulting | Development

XQuery by Priscilla WalmsleyGet the book!
XQuery by Priscilla Walmsley