XPath/XQuery contains-token function


Determines whether or not any of the supplied strings, when tokenized at whitespace boundaries, contains the supplied token, under the rules of the supplied collation.


$input as xs:string*,
$token as xs:string
) as xs:boolean
$input as xs:string*,
$token as xs:string,
$collation as xs:string
) as xs:boolean


The two-argument form of this function is deterministic, context-dependent, and focus-independent. It depends on collations.
The three-argument form of this function is deterministic, context-dependent, and focus-independent. It depends on collations static-base-uri.


If $input is the empty sequence, the function returns false.

Leading and trailing whitespace is trimmed from the supplied value of $token. If the trimmed value of $token is a zero-length string, the function returns false.

The collation used by this function is determined according to the rules in .

The function returns true if and only if there is string in $input which, after tokenizing at whitespace boundaries, contains a token that is equal to the trimmed value of $token under the rules of the selected collation.

That is, the function returns the value of the expression:

some $t in $input!fn:tokenize(.) satisfies 
                 compare($t, fn:replace($token, '^\s*|\s*$', ''), $collation) eq 0)


The expression fn:contains-token("red green blue ", "red") returns true().

The expression fn:contains-token(("red", "green", "blue"), " red ") returns true().

The expression fn:contains-token("red, green, blue", "red") returns false().

The expression fn:contains-token("red green blue", "RED", "http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive") returns true().


Interior whitespace within $token will cause the function to return false, unless such whitespace is ignored by the selected collation.

This function can be used for processing space-separated attribute values (for example, the XHTML and DITA class attribute), where one often needs to test for the presence of a single token in a space-separated list. The function is designed to work both when the attribute has been validated against an XSD list type, and when it appears as a single untyped string. It differs from the HTML 5 definition in that HTML 5 recognizes form feed (x0C) as a separator. To reproduce the HTML token matching behavior, the HTML ASCII case-insensitive collation should be used: see .