XML:返回发生一定次数的序列元素

时间:2015-10-03 15:10:25

标签: xml xquery

我有一个包含电影信息的XML文件。我希望我的表达式能够返回XML中导演超过1部电影的导演。

我现在拥有的是:

for $director in //director
where count($director) > 1
return $director 
This does not produce anything. 

我想做的是:

For every director in the XML: 
return director's name IF the director's name occurs more than once in //director

你会如何解决这个问题?

XML示例:

<result>
    <videos>
        <video id="id1235AA0">
            <title>The Fugitive</title>
            <genre>action</genre>
            <rating>PG-13</rating>
            <summary>Tommy Lee Jones and Harrison Ford are the hunter and the hunted in this fast-paced story of a falsely convicted man who escapes to find his wife's true killer.</summary>
            <details>Harrison Ford and Tommy Lee Jones race through the breathless manhunt movie based on the classic TV series. Ford is prison escapee Dr. Richard Kimble, a Chicago surgeon falsely convicted of killing his wife and determined to prove his innocence by leading his pursuers to the one-armed man who actually commited the crime.</details>
            <year>1997</year>
            <director>Andrew Davis</director>
            <studio>Warner</studio>
            <user_rating>4</user_rating>
            <runtime>110</runtime>
            <actorRef>00000003</actorRef>
            <actorRef>00000006</actorRef>
            <vhs>13.99</vhs>
            <vhs_stock>206</vhs_stock>
            <dvd>14.99</dvd>
            <dvd_stock>125</dvd_stock>
            <beta>1.03</beta>
            <beta_stock>12</beta_stock>
            <LaserDisk>12.00</LaserDisk>
            <LaserDisk_stock>10</LaserDisk_stock>
        </video>
    </videos>
</result>

2 个答案:

答案 0 :(得分:0)

试试这个:

for $director in distinct-values(//director)
where count(//video[director = $director]) > 1
return $director

答案 1 :(得分:0)

我用以下代码解决了这个问题:

let $directors := distinct-values(
for $director in //director
where count(result/videos/video[director = $director]) > 1
return $director
)
return $directors