XQuery中的多个for循环实现

时间:2018-03-19 17:24:36

标签: xquery osb

以下是我的示例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>

我需要一个返回

的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 Developer&#39; s ..................,Legacy .................. ..,孩子们的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>

我们在多个for循环实现中遇到问题。

非常感谢任何帮助。 提前致谢

1 个答案:

答案 0 :(得分:0)

从问题和评论中你似乎想要这样的东西:

<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>

根据您的输入,结果是:

<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>

我的直觉告诉我你可能不想要data()部分。如果没有它,你会得到:

<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>