Search code examples
xmlxquerydistinct-values

xquery: how to get distinct values of nodes


I'd like to know if there is any function for Xquery similar to distinct-values but that returns a node.

Let me be clearer: for example I have a bibliography and for each author in it I want to list all the books he wrote. The author element in my specific case is like this:

<author>
  <last> Shakespear </last>
  <first> William </first>
</author>

Using distinct-values on author returns ShakespearWilliam so as far as I can see it does not help. I'd like a function that preserves the structure of the element author without considering duplicates.

If you find another way for the query then let me know. Does anyone have any idea?


Solution

  • XQuery 3.0 has a "group by" construct, and this allows you for example to group authors by the value of (first name, last name). When you have grouped the nodes, you essentially have your answer: nodes are distinct if and only if they are in different groups.

    There are quite a few products around that implement this part of the XQuery 3.0 draft; Saxon 9.4 is one of them.