Search code examples
xquery

XQuery, get element with same value?


xml

<books> <book> <author>Gambardella, Tim</author> </book> <book> <author>Knorr, Stefan</author> </book> <book> <author>O'Brien, Tim</author> </book> </books>

How get authors with same name?

I tried searching but didn't find even working examples for finding the same strings


Solution

  • Usually this is a grouping problem e.g.

    for $author in books/book/author 
      group by $prename := $author => substring-after(',') => normalize-space() 
    return 
      <group prename="{$prename}">{$author}</group>