Dylan
Dylan

Reputation: 63

xQuery: Check for string in element but ignore multiple elements within the same element but with different values

Okay, so I have this movie xml file that contains two movies. I want to get all movies that contain the 'Action' string.

<movie id="19995">
    <genre genre_id="28">Action</genre>
    <genre genre_id="12">Adventure</genre>
    <genre genre_id="14">Fantasy</genre>
    <vote_average>7.2</vote_average>
</movie>

<movie id="19995">
    <genre genre_id="14">Fantasy</genre>
    <genre genre_id="878">Science Fiction</genre>
    <vote_average>7.2</vote_average>
</movie>

This is my code atm.

let $xml_file := doc("tmdb_5000_movies")
let $action_movies := $xml_file//movie[contains(genre/text(), 'Action')]

But I get the following error message.

[XPTY0004] Item expected, sequence found: (text {"Action"}, text {"Adventure"}, ......

Upvotes: 1

Views: 279

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24928

contains() indeed takes one item; but since each movie in the xml has two or more genres, contains encounters a sequence of two or three items, resulting in the error message.

Try it this way:

let $action_movies := $xml_file//movie

return  $action_movies[genre[contains(text(),'Action')]]

Upvotes: 3

Related Questions