0

$a is

(<pet name="a1">
<age num="1"/>
</pet>,
<pet name="a2">
<age num="2"/>
</pet>)

$b is

(<pet name="a2">
<age num="9"/>
</pet>,
<pet name="a3">
<age num="11"/>
</pet>
<pet name="a3">
<age num="14"/>
</pet>

)

I am trying to get the following as output (Everything in $a and everything in $b except those already printed out) i.e,

(<pet name="a1">
<age num="1"/>
</pet>,
<pet name="a2">
<age num="2"/>
</pet>,
<pet name="a3">
<age num="11"/>
<age num="14"/>
</pet>)

Since <pet name="a2"> is already printed from $a, the same element with same attribute in $b must be skipped, I tried the below code in xquery, but I am unable to get the required result.

for $l in $a
    for $m in $b
      return if ($m/@name!=$l/@name)
NEO
  • 1,961
  • 8
  • 34
  • 53

1 Answers1

1

This can be solved more simply without loops by joining the sequences and filtering the second using a predicate expression:

($a | $b[not(@name = $a/@name)])
wst
  • 11,681
  • 1
  • 24
  • 39
  • I have edited the question and added another sub element in $b. I tried "group by @ name ($a | $b[not(@ name = $a/@ name)])", to group elements with same @ name in the output. Is this the correct approach? – NEO Oct 12 '14 at 20:24
  • @thefragmenter No, to use `group by` or `order by` you must use a `for ...` (loop) expression. However, in your example grouping by @name wouldn't do anything, since the expression is designed such that all @names are unique. – wst Oct 12 '14 at 21:05