[xquery-talk] when to use document { }?

Jens Teubner jens.teubner at in.tum.de
Tue Jul 26 13:42:29 PDT 2005

On Mon, Jul 25, 2005 at 05:33:52PM -0700, Howard Katz wrote:

> I just came across an example of a query being used to generate an xhtml
> document where the query is wrapped in a document constructor  :
> [...]
> What is the document constructor doing in this case? The query would
> generate perfectly good xhtml without it, would it not? I know that Saxon
> automatically applies document construction to any document being serialized
> (unless you turn it off), but I've seen lots of perfectly valid xml/xhtml
> generated without its use in other environments. What's the difference
> between the two cases?

Well, the *serialized output* of that query may be the same with or
without the document constructor, depending on your query engine.

However, in the XQuery data model, the application of the document {}
constructor makes a big difference. Consider, e.g. the queries

  let $a := <a/>
    return $a/a


  let $a := document { <a/> }
    return $a/a

In the former case, $a is bound to the element a. If you do a child step
on that (empty) element, you will get an empty result.

In the latter case, $a will be bound to a document node that sits on top
of the (empty) element <a/>. If you now do a child step from $a, you
will happliy get the <a/> element.

Does this make the document {} constructor clearer?


Jens Teubner
Technische Universitaet Muenchen, Department of Informatics
D-85748 Garching, Germany
Tel: +49 89 289-17259     Fax: +49 89 289-17263

Unix is user friendly - it's just picky about it's friends.

More information about the talk mailing list