如何从XQuery中的重复节点中检索最大值?

问题描述 投票:0回答:1

我有一个XML文件。我想从重复值中检索最大值节点(在以下情况下,authortitlegenre字段相同,但是number字段不同)。

<?xml version="1.0"?>
<catalog>
   <book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <number>1234</number>
   </book>
   <book>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <number>1235</number>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <number>1236</number>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <number>1237</number>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <number>1238</number>
   </book>
   <book>
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <number>1239</number>
   </book>
   <book>
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <number>1240</number>
   </book>
   <book>
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <number>1246</number>
   </book>
   <book>
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <number>1247</number>
   </book>
   <book>
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <number>1248</number>
   </book>
   <book>
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <number>1241</number>
   </book>
   <book>
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <number>1242</number>
   </book>
   <book>
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <number>1243</number>
   </book>
   <book>
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <number>1244</number>
   </book>
   <book>
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
     <number>1245</number>
   </book>
</catalog>

这里是预期的输出:

<?xml version="1.0"?>
<catalog>
   <book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <number>1234</number>
   </book>
   <book>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <number>1235</number>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <number>1236</number>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <number>1237</number>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <number>1238</number>
   </book>
   <book>
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <number>1239</number>
   </book>
   <book>
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <number>1248</number>
   </book>
   <book>
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <number>1241</number>
   </book>
   <book>
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <number>1242</number>
   </book>
   <book>
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <number>1243</number>
   </book>
   <book>
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <number>1244</number>
   </book>
   <book>
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
     <number>1245</number>
   </book>
</catalog>

作者是Thurman, Paula。应检索最大数量的节点。有什么办法可以检索输出?如果是这样,请引导我朝正确的方向发展。

提前感谢!

xml xquery xquery-3.0
1个回答
2
投票

您可以按包含所有重复值(作者,书名,体裁)的字符串对书籍进行分组,并以最大数量返回该组中的书籍:

element catalog {
  for $book-group in catalog/book
  group by $key := string-join($book-group/(author, title, genre), '|')
  let $max-number := max($book-group/number)
  return $book-group[number = $max-number]
}
© www.soinside.com 2019 - 2024. All rights reserved.