Reputation: 67
I have an XML file. I wanted to retrieve the largest value node from the duplicate values (in the case below, author
, title
, and genre
fields are the same, but the number
field is different).
<?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>
Here is the expected output:
<?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>
Where the author is Thurman, Paula
. The largest number of the node should be retrieved. Is there any way that I can retrieve the output? If so, please guide me in the right direction.
Thanks in advance!!
Upvotes: 0
Views: 37
Reputation: 6229
You can group your books by a string that comprises all duplicate values (author, title, genre) and return the book in that group which has the maximum number:
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]
}
Upvotes: 3