RE: XQuery's FLWR expression vs. XSLT down-reference pull

I think it may be helpful if I provide some more concrete examples of what I
addressed in my last posting.

If the XPath notion of context is to be satisfied in XQuery, something like
the following must be possible:

FOR $b IN document("bib.xml")//book
WHERE publisher = "Morgan Kaufmann"
AND year = "1998"
RETURN title

This version is certainly better:

document("bib.xml")//book[year="1998" and  publisher="MK"]/title

However, joins are not the only reason why I might need to use a FLWR
expression. Thus, variable references should not always be necessary. For
example:

FOR . IN document("bib.xml")//book
RETURN
  <publication>
    title,
    author
  <publication>

Or a better syntax might be the following (this would take away the
implication of the above that "." needs to be explicitly bound, which is
never the case in XPath):

FOR-EACH document("bib.xml")//book
RETURN
  <publication>
    title,
    author
  <publication>


Replace "FOR-EACH" with whatever you like. The point is that "." should not
have to be explicitly bound. My original syntax suggestion was probably
misleading in this regard.

XPath 1.0 has some problems with regard to casting and its four node types.
These semantics need to be cleaned up. I acknowledge that many of these
problems will be solved in XPath 2.0.

However, XPath 1.0's notion of context is *not* broken. So there should be
no reason to change this in XPath 2.0, or for that matter, XQuery. Thus,
this requires that there always be a defined context for the evaluation of
XPath expressions. A set of variable bindings is part of that context, but
it is not all of that context. There also needs to be a "context node". My
suggestion is that XQuery simply borrow XSLT's notion of a "current node"
and use that as the context node for XPath evaluation.

XPath defines the following context for expression evaluation:

<quote numbers="added"
href="http://www.w3.org/TR/xpath#section-Introduction">

1. a node (the context node)

2. a pair of non-zero positive integers (the context position and the
context size)

3. a set of variable bindings

4. a function library

5. the set of namespace declarations in scope for the expression
</quote>

XQuery seems to provide all but the first. By satisfying the first, XPath
expressions may be resolved without always having to use variable
references. A very straightforward way of doing this is by adopting XSLT's
concept of a "current node" and using that as the context node for XPath
expression evaluation.

<quote numbers="added" href="http://www.w3.org/TR/xslt#section-Expressions">
In XSLT, an outermost expression (i.e. an expression that is not part of
another expression) gets its context as follows:

1. the context node comes from the current node

2a. the context position comes from the position of the current node in the
current node list; the first position is 1

2b. the context size comes from the size of the current node list

3. the variable bindings are the bindings in scope on the element which has
the attribute in which the expression occurs (see [11 Variables and
Parameters])

4. the function library consists of the core function library together with
the additional functions defined in [12 Additional Functions] and extension
functions as described in [14 Extensions]; it is an error for an expression
to include a call to any other function

5. the set of namespace declarations are those in scope on the element which
has the attribute in which the expression occurs; this includes the implicit
declaration of the prefix xml required by the the XML Namespaces
Recommendation [XML Names]; the default namespace (as declared by xmlns) is
not part of this set
</quote>

XQuery could already easily define, in its own terms, this list for 2-5 as
follows:

2a. the context position comes from the position of the current
binding-tuple in the current list of binding-tuples; the first position is 1

2b. the context size comes from the size of the current list of
binding-tuples

3. the variable bindings are the bindings in scope within the current FLWR
expression

4. the function library consists of the core function library together with
additional functions defined in ____, as well as user-defined functions
declared in FUNCTION declarations; it is an error for an expression to
include a call to any other function

5. the set of namespace declarations are provided by (global?) NAMESPACE
declarations


XQuery, as I said before, could satisfy #1 by adopting XSLT's "current node"
and "current node list". Thus, "binding-tuple" in #2 and #3 above would be
changed to, simply, "node".

Within this framework,

FOR $b IN document("bib.xml")//book

would essentially be shorthand for:

FOR-EACH document("bib.xml")//book
LET $b := .

Again, the particular syntax used for "FOR-EACH" is insignificant.

For joins, you could continue to use this shorthand; variable references
will be necessary, because once you enter a new FOR clause, the current node
of the previous FOR clause will be lost.

Finally, for referencing the current node in the context of an XPath
predicate, I suggest simply borrowing the current() function from XSLT. The
use of "." should *not* be used to represent the current node, as "."
*always* means the context node, whether the context node happens to be the
current node or not.

I hope this helps clarify my position,

Evan Lenz
XYZFind Corp.

Received on Friday, 23 February 2001 20:26:32 UTC