home contribute faq download

FunctX XSLT Functions

functx:contains-word

Whether one string contains another, as a separate word

Google
Webxsltfunctions.com

Description

The functx:contains-word function returns true if $word is contained in $arg as a separate word. It must be delimited by non-word characters or the beginning or end of $arg. It is case insensitive; it matches a word even if the case is different.

A "non-word character" is one that in Unicode belongs in either the Punctuation, Separators or Other category. Generally, most punctuation and white space are considered non-word characters, while letters and digits are word characters.

Arguments and Return Type

NameTypeDescription
$arg xs:string? the string to search
$word xs:string the word to find
return value xs:boolean

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:contains-word" as="xs:boolean"
              xmlns:functx="http://www.functx.com">
  <xsl:param name="arg" as="xs:string?"/>
  <xsl:param name="word" as="xs:string"/>

  <xsl:sequence select="
   matches(upper-case($arg),
           concat('^(.*\W)?',
                     upper-case(functx:escape-for-regex($word)),
                     '(\W.*)?$'))
 "/>

</xsl:function>

Examples

XSLT ExampleResultsExplanation
<functx:contains-word('abc def ghi', 'def'>
<true>rue
<functx:contains-word('abc.def\ghi', 'def'>
<true>rue
<functx:contains-word('abc def ghi', 'abc'>
<true>rue
It can be at the beginning or end.
<functx:contains-word('abc', 'abc'>
<true>rue
It can be the whole value.
<functx:contains-word('abcdef', 'abc'>
<false>alse
abc does not appear as a separate word.

Depends On

functx:escape-for-regexEscapes regex special characters

See Also

fn:containsWhether one string contains another

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Datypic XSLT Services

Recommended Reading:

XQuery