[xquery-talk] 'except' in XQuery

Michael Kay mhk at mhk.me.uk
Tue Jan 11 17:57:54 PST 2005

> fn:document("docs/xmark.xml")//* except
> fn:document("docs/xmark.xml")/site/people

This selects a sequence of nodes that doesn't include the /site/people
elements. But it does include the site element; and when you serialize the
result of the query, it will typically display each selected element
together with all its descendants. Typically this will contain many
duplicates, since nodes will be shown both because they were themselves
selected, and also because they're part of a tree whose root element was

Copying a tree with one branch omitted is actually a little tricky in
XQuery. What you need is a recursive function that copies a node together
with all its children, except for the child to be omitted:

declare function copy-except($n as node(), $omit as node()) {
  element {node-name($n)} {
    for $c in ($n/child::node() except $omit)
    return copy-except($c, $omit)


Michael Kay

More information about the talk mailing list