Как получить наибольшее значение из дублирующих узлов в XQuery? - PullRequest
0 голосов
/ 10 февраля 2020

У меня есть XML файл. Я хотел получить узел наибольшего значения из повторяющихся значений (в приведенном ниже случае поля author, title и genre совпадают, но поле 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. Наибольшее количество узлов должно быть восстановлено. Есть ли способ, которым я могу получить вывод? Если это так, пожалуйста, направьте меня в правильном направлении.

Спасибо заранее !!

1 Ответ

3 голосов
/ 10 февраля 2020

Вы можете сгруппировать свои книги по строке, содержащей все повторяющиеся значения (автор, название, жанр), и вернуть книгу в этой группе с максимальным числом:

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]
}
...