Reputation: 97
I want to write an XQuery query that returns XML elements that have any XML element containing a keyword, say 'countdown'. I wrote following query and is not working:
for $entity in collection('data2.dbxml')//movie
where contains( $entity/ * , 'countdown' ) return $entity
Why, and how to write the correct query?
An example XML document is
<?xml version="1.0" encoding="UTF-8"?>
<movies xmlns:xlink= " http://www.w3.org/1999/xlink " >
<movie>
<title> " 100 Jahre - Der Countdown " (1999) {1998 - Der Präsident und das Mädchen (#10.8)}
</title>
<url>http://www.imdb.com/Title? " 100 Jahre - Der Countdown " (1999) {1998 - Der Präsident und
das Mädchen (#10.8)}
</url>
<overview>
<releasedates>
<releasedate>Germany 30 December 1999
</releasedate>
</releasedates>
</overview>
<cast>
<actors>
<actor>
<name>Brückner Christian
</name>
<character>Narrator
</character>
</actor>
<actor>
<name>Clinton Bill (I)
</name>
<character>(archive footage) Himself
</character>
</actor>
<actor>
<name>Lewinsky Monica
</name>
<character>(archive footage) Herself
</character>
</actor>
</actors>
</cast>
<additional_details>
</additional_details>
<fun_stuff>
</fun_stuff>
</movie>
</movies>
Upvotes: 0
Views: 212
Reputation: 1273
Ideally you would want to use XQuery full text for this because it will take are of case sensitivity, etc:
for $entity in collection('data2.dbxml')//movie
where $entity contains text 'countdown'
return $entity
If you don't have access to it then use the answer Jens provided.
Upvotes: 0
Reputation: 38682
XQuery is case sensitive, "Countdown" and "countdown" are not the same. Furthermore, contains($string, $needle)
expects a single string, and not a sequence. Luckily, XQuery implicitly casts any element including its subtree to a string by concatenating all text nodes.
for $movie in collection('data2.dbxml')//movie
where contains(lower-case($movie), 'countdown')
return $movie
Upvotes: 1