[xquery-talk] two xml match/diffes

James A. Robinson jim.robinson at stanford.edu
Tue Jan 16 23:07:59 PST 2007


> <result>
> <same><s1>1</s1><s2>3</s2></same>
> <root1><a3>5<a3></root1>
> <root2><b2>2</b2></root2>
> </result>
> 
> How can I do this kind of query?

There is probably a better way to do this, but here is one way I came
up with.  You compute the data values common to $a and $b and then use
that to output <same/>, then you use is to figure out the non-similar
items in $a and $b:

let $a := doc('a.xml')/*
let $b := doc('b.xml')/*
let $c := $a/*[. = $b/*]
return (
  <result>{
    <same>{
      for $s at $i in $c
      return element {concat('s', $i)} {$s/data(.)}
    }</same>,
    element {node-name($a)} {
      $a/*[not(. = $c)]
    },
    element {node-name($b)} {
      $b/*[not(. = $c)]
    }
  }</result>
)

This is doing three many-to-many checks, I'm not sure how else one can
do this query, so I'd be interested in seeing what others come up with.
I'm making an assumption that the many-to-many check of something like
$b/*[not(.=$c)] is no less efficent than doing a loop performing a
per-item check of each item in $b to see if it is in $c.

Jim

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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