xpath - How to make a for loop work for xquery, in the context of 'contains'? -
if have books.xml file like
<books> <book isbn="123"> <collections> <collection c="1" /> <collection c="2" /> <collection c="3" /> <collection c="4" /> </collections> </book> <book isbn="234"> <collections> <collection c="1" /> <collection c="2" /> <collection c="3" /> <collection c="5" /> </collections> </book> </books> i want return isbns both books belong same collections.
declare variable $doc := doc("books.xml"); <samecollections> { $b1 in $doc/books/book $b2 in $doc/books/book $b1 << $b2 , every $p in $b1/collections/collection satisfies $p in $b2/collections/collection return ... } </samecollections> how make work? i've tried inserting loop inside loop, nothing worked, think i'm misunderstanding syntax of xquery. help?
ideally, xquery processor supports group by, answer simple:
for $b in $books/book let $colls-key := string-join($b/collections/collection/@c, '') group $colls-key return element grouped { $b/@isbn/string() } if collections aren't ordered in data, can order them before generating key:
let $colls-key := string-join( $c in $collections/collection/@c order $c return $c) without group by it's little messier.
declare function local:key($collections){ string-join($collections/collection/@c, '') }; $key in distinct-values($doc/books/book/local:key(collections)) return element grouped { $doc/books/book[local:key(collections) eq $key]/@isbn/string() } depending on xquery processor may less efficient, it's not advisable large data sets. if performance issue, can optimizations available specific implementation.
Comments
Post a Comment