Pergunta

When I try to execute my Xquery Code on xml file, I am getting multiple results in one of my fields.

Here is my xml file

<Actors>
<Actor name="NTR">
    <Movie  TITLE="Yamadonga" Director="Rajamouli"></Movie>
        <Movie TITLE="AADI" Director="VV vinayak">
    </Movie>
</Actor>
<Actor name="Rajeev">
    <Movie  TITLE="Yamadonga" Director="Rajamouli" ></Movie>

</Actor>
<Actor name="mahesh">
    <Movie  TITLE="pokiri" Director="puri">
    </Movie>
</Actor>

my xquery file

<Director>
{
for $Movie in doc("actors.xml")/Actors/Actor/Movie
return
if($Movie/@TITLE=$title)
then
data($Movie/@Director)
else()
}
</Director>

Most importantly, my result

<movies>
   <movie>
       <Title>Yamadonga</Title>
       <Actor>NTR</Actor>
       <Actor>Rajeev</Actor>
       <Director>Rajamouli Rajamouli</Director>
   </movie>
</movies>

How to get only one value in the director field?

My procedure :- I ran the distinct values function over (../Movie/@TITLE) and that gave me the answer for displaying title. But as title and director are attributes of movie, I cannot access one using the other. When I iterate over actor, as there are two actors having a single director for single movie, the director name gets printed twice. When I iterate over movie, I cannot use distinct-values over it as it is not an attribute.

Foi útil?

Solução

Your XQuery is really not very efficient or easily readable. You can do a simple xpath:

<Director>
{
data((doc("actors.xml")/Actors/Actor/Movie[@TITLE = $title])[1]/@Director)
}
</Director>

Outras dicas

It's because the for is returning 2 movies. Why don't you just use an XPath with distinct-values()?

<Director>
{
distinct-values(doc("actors.xml")/Actors/Actor/Movie[@TITLE=$title]/data(@Director))
}
</Director>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top