tags:

views:

148

answers:

2

hi I have a bit lame question, can't find the answer though.

what happens if I leave the constructor brackets void?

e.g.

< p> {data(doc("somedata.xml")//node[0])}< /p>

I mean, I know what happens, but what is it considered as when being parsed?

is it like ?

{ for $i in "0" return data(doc("somedata.xml")//node[0]) }

A: 

I don't really understand your question. What do you mean by "leave the constructor brackets void"?

The query you gave is an node whose contents are an expression. It is parsed as such. It is a common misconception that every expression is a FLWOR expression. A FLWOR expression is just another expression.

So,

<p> {data(doc("somedata.xml")//node[0])}</p>

is simply parsed as

<p> {data(doc("somedata.xml")//node[0])}</p>

Just like how

Console.WriteLine("foo");

is not interpreted as

foreach (int x in new string[] {"0"})
  Console.WriteLine("foo")

in C#.

If you want the full Formal Semantics expansion, which is one way in which the query can be interpreted, then it looks something like this:

element {p}
{
  fs:item-sequence-to-node-sequence(
    fn:data(
      fs:distinct-doc-order-or-atomic-sequence(
        let $fs:sequence :=
          fs:distinct-doc-order-or-atomic-sequence(
            let $fs:sequence := doc("somedata.xml")
            let $fs:count := count($sequence)
            for $fs:dot at $fs:position in $fs:sequence
            return $fs:dot/descendant-or-self::node())
        let $fs:count := count($fs:sequence)
        for $fs:dot at $fs:position in $fs:sequence
        return item-at($fs:dot/child::node, 0)
      )
    )
  )
}
Oliver Hallam
what I ment by void brackets is just brackets without any FLWOR construction. Is such a term in xquery just a shorter version of something longer which is parsed the same way?
stupid_idiot
+1  A: 

It is a common misconception that any XQuery is a FLWOR expression. This misconception comes when people approach XQuery from a SQL perspective, treating it as a SELECT.

This is in fact not the case; a FLWOR expression is in many ways just another expression. It may be that a FLWOR expression is executed as a SQL expression, but this doesn't have to be the case.

XQuery can be viewed as a functional programming language (like Haskell) that happens to have some declarative constructs (like where and order by).

The expression 1+2 is just an XQuery expression that adds the numbers 1 and 2, there does not need to be an implicit FLWOR expression around it.

If you wanted to consider XQuery in a fully tuple-based algebra then you could consider the input to be a single empty tuple. By this I mean the following.

Look at this query:

for $x in ...
for $y in ...
where $x/@name=$y/@name
return $x

If you were considering this in a tuple based algebra, the input to the for expression would be a stream of tuples defining $x and $y. It is obvious how this could relate to a database query. This corresponds to a table with two columns $x and $y and a row for each pair that have equal names.

You could consider the following query

//foo

as operating on a single tuple with no values. This would be a little bit like a FLWOR expression with no fors or lets (just a return expression, if that were allowed). In relational land this would be a table with no columns and one row. However this is just a logical abstraction, and most (if not all) XQuery implementations represent this as just an expression.

Oliver Hallam