質問

In the play (http://www.ibiblio.org/xml/examples/shakespeare/as_you.xml), I am looking to get the lines spoken by SPEAKERS, that contain the word love in it. (i.e. it love as in loved, should not be displayed). In the output, CHARLES is the name of the speaker, PLAY is the title of the play, followed by the line containing the word love in it, spoken by that person.

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

<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>
        ...

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

Any guidance will be be appreciated.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top