XQuery中的多循环实现

XQuery中的多循环实现,xquery,osb,Xquery,Osb,下面是我的示例XML <catalog> <book> <author>Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>20

下面是我的示例XML

<catalog>
   <book>
      <author>Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>Ralls, Kim</author>
      <title>Rain Fantasy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>zxcv</author>
      <title>Maeve</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>zxcv</author>
      <title>Legacy</title>
      <genre>Fiction</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>The</title>
      <genre>Fiction</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>Horror</author>
      <title>Horror</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When abc meets xyz</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>O'Brien, Tim</author>
      <title>kids ganes</title>
      <genre>story</genre>
      <price>36.95</price>
      <publish_date>2000-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-01</publish_date>
      <description>The abc</description>
      <storeno>123</storeno>
   </book>
   <book>
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>story</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio</description>
      <storeno>123</storeno>
   </book>
</catalog>

马修
XML开发人员指南
电脑类
44.95
2000-10-01
深入了解如何创建应用程序
使用XML。
123
拉尔斯,金
雨幻想
幻想
5.95
2000-12-16
前任
123
zxcv
梅夫
幻想
5.95
2000-11-17
在纳米技术崩溃之后
123
zxcv
遗产
小说
5.95
2001-03-10
在后启示录中
123
科雷茨,伊娃
这个
小说
5.95
2001-09-10
两个女儿
123
恐怖
恐怖
恐怖
4.95
2000-09-02
当abc遇到xyz时
123
克诺尔,斯特凡
令人毛骨悚然的爬虫
恐怖
4.95
2000-12-06
关于蟑螂的恐怖故事选集,
蜈蚣、蝎子和其他昆虫。
123
蒂姆·奥布莱恩
ganes的孩子们
故事
36.95
2000-12-09
微软的.NET计划在
详细信息请参阅本深度程序员参考资料。
123
蒂姆·奥布莱恩
MSXML3:综合指南
计算机
36.95
2000-12-01
美国广播公司
123
加洛斯,迈克
VisualStudio7:综合指南
故事
49.95
2001-04-16
Microsoft Visual Studio
123
我需要一个返回的XQuery

<titles>
need the first instance of the title where the genre is “Fantasy”
need all the titles concatenated where the genre is “Computer”
need all the titles concatenated where the genre is “Fiction”
need the first instance of the title where the genre is “Story”
</ titles >

需要标题的第一个实例,其中类型为“幻想”
需要将所有类型为“计算机”的标题连接起来
需要将所有类型为“小说”的标题连接起来
需要标题的第一个实例,其中类型为“故事”
示例:(Rain Fantasy,XML开发人员指南,Legacy,kids ganes) 注:在上述情况下,可忽略该情况进行比较

这就是我们正在尝试的

<Titles>
          let $fan := $catalog /book[genre = ‘Fantasy’][1]/title
          let $stry := $catalog /book[genre = ‘Story’][1]/title
          for $comp in $catalog /book[genre ='Computer']/title
          return concat($comp, “”)
          for $fict in $catalog /book[genre ='Fiction']/title
          return concat($fict, “”)

          concat($fan, $comp, $fict, $stry)
</Titles>

let$fan:=$catalog/book[流派='Fantasy'][1]/title
让$stry:=$catalog/book[流派='Story'][1]/标题
对于$catalog/book[genre='Computer']/title中的$comp
返回concat($comp,“”)
在$catalog/book[genre='Fiction']/title中的$fict
返回concat($fict,“”)
concat($fan、$comp、$fict、$stry)
我们在实现多个循环方面面临问题

非常感谢您的帮助。
提前感谢您的提问和评论,您似乎想要这样的内容:

<Titles>
{
    for $genre in distinct-values($catalog/book/genre)
    let $books := $catalog/book[genre=$genre]
    let $retval := if($genre=("Fantasy","Story")) then $books[1]/title else $books/title
    return data($retval)
}
</Titles>

因为有很多方法可以实现这一点,如果您能在这里发布您期望的确切结果,这将非常有帮助注意:在上面的比较中可以忽略该案例。因此,您希望
..
作为结果的一部分?Rain FantasyXML开发人员指南MSXML3:综合指南ganes-预期输出
<Titles>XML Developer's Guide Rain Fantasy Legacy The Horror Creepy Crawlies kids ganes Visual Studio 7: A Comprehensive Guide MSXML3: A Comprehensive Guide</Titles>
<Titles>
  <title>XML Developer's Guide</title>
  <title>Rain Fantasy</title>
  <title>Legacy</title>
  <title>The</title>
  <title>Horror</title>
  <title>Creepy Crawlies</title>
  <title>kids ganes</title>
  <title>Visual Studio 7: A Comprehensive Guide</title>
  <title>MSXML3: A Comprehensive Guide</title>
</Titles>