搜索特定单词(XQUERY)

时间:2014-03-23 12:03:58

标签: xquery

在剧中(http://www.ibiblio.org/xml/examples/shakespeare/as_you.xml),我希望得到SPEAKERS所说的内容,其中包含爱这个词。 (即它喜欢爱,不应该显示)。在输出中,CHARLES是演讲者的名字,PLAY是剧本的标题,后面是包含爱的词的行,由该人说出。

==============================

<line speaker="CHARLES" play="As You Like It">for your love, I would be loath to foil him, as I</line>
<line speaker="CHARLES" play="As You Like It">out of my love to you, I came hither to acquaint you</line>
<line speaker="OLIVER" play="As You Like It">Charles, I thank thee for thy love to me, which</line>
<line speaker="CELIA" play="As You Like It">that I love thee. If my uncle, thy banished father,</line>
        ...

==============================

任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下内容应该有效:

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>

函数local:contains-word()直接取自优秀的FunctX库,有关详细信息,请参阅http://www.xqueryfunctions.com/xq/functx_contains-word.html。当然,您也可以简单地导入完整的库并使用给定的函数。