[xquery-talk] Xquery+ reduce two docs into one

James A. Robinson jim.robinson at stanford.edu
Fri Jan 12 11:49:49 PST 2007


 
> Hello - The result was just one empty meta element. Something like
> this:
> 
> <FF>
>     </meta>
> </FF>

That's strange, I got the right results when I ran his solution. Are
you sure you updated the filenames properly?

I thought that solution looked pretty good, though the thing that
irritated me (other than the multiple many-to-many operations) was the
fact that we were both limiting ourselves to 2 input documents.

A slightly extended version that I think will work if one needs to
add new documents would be something that understands the concepts
behind the fn:collection() function. But anyway, I hope others can
point out if there is a way to do this without multiple many-to-many
type joins (which I think both David and my solution require).

declare namespace f="uri.local-functions";

(: simulate a DB where we have a collection() function and indicies :)
declare function f:collection() {
  (doc("sebi-1.xml"), doc("sebi-2.xml"))
};

(: count the number of documents :)
let $total          := count(f:collection())
(: load all metadata :)
let $meta           := f:collection()/*/meta
(: find unique filenames in all metadata  :)
let $files          := distinct-values(f:collection()/*/meta/FileName)
(: find all filenames which are in all metadata :)
let $file_intersect := (
  for $f in $files
  return (
    let $meta := f:collection()/*/meta[FileName = $f]
    where (count($meta) = $total)
    return $f
  )
)
(: find all metadata with the intersecting files :)
let $meta_intersect := (
  for $m in $meta[FileName = $file_intersect]
  return
    $m/*[not(self::FileName)]
)
(: and return the new document :)
return (
  <FF>
   <meta>{
     
     for $m in $meta_intersect
     order by local-name($m)
     return $m,
     
     for $f in $file_intersect
     return <FileName>{$f}</FileName>

   }</meta>
 </FF>
)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson at stanford.edu
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)


More information about the talk mailing list