The following should work:
declare function local:escape-for-regex
( $arg ) as xs:string {
replace($arg,
'(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')
} ;
declare function local:contains-word
($arg as xs:string,
$word as xs:string) as xs:boolean {
matches(
upper-case($arg),
concat('^(.*\W)?',
upper-case(local:escape-for-regex($word)),
'(\W.*)?$'
))
};
for $play in doc('http://www.ibiblio.org/xml/examples/shakespeare/as_you.xml')/PLAY
for $l in $play//LINE
where $l[local:contains-word(., 'love')]
return <line speaker="{$l/preceding-sibling::SPEAKER}" play="{$play/TITLE}">{$l/text()}</line>
The function local:contains-word()
is directly taken from the excellent FunctX library, see http://www.xqueryfunctions.com/xq/functx_contains-word.html for more information. Of course you can also simply import the complete library and use the given function.