[xquery-talk] RE: FLOR or FLWOR

Hans-Juergen Rennau hrennau at yahoo.de
Mon Sep 15 05:54:42 PDT 2008


Hello, Rob,

it seems to me important to remember two facts about FLWOR expressions:
a) they may not only create loops but procure intermediate results (via let clause)
b) to remove a tuple from the tuple stream, a predicate has to be placed in a for clause

If the filter condition refers to such an intermediate result, it is very convenient that we have the where, because otherwise we would have to create the intermediary twice: once to procure the predicate (as part of a for loop), and once to be used later on (as a let clause).

As an illustration consider this query:

(:---------------------------------------------------------------------------------------:) 
  string-join(
   for $owner in distinct-values(//ContainerDataList/@EIGT)
   let $nrs := distinct-values(//ContainerDataList[@EIGT eq $owner]/@CONR)
   where count($nrs) le 2
   order by $owner 
   return (
     concat($owner, "  (", count($nrs), ")"),
     for $n in $nrs return concat("            ", $n)
   )
, "
")
(:---------------------------------------------------------------------------------------:) 


 The intermediate ($nrs) is definitely needed later on, and the filter condition refers to it. To replace the where by a predicate, one would end up with something like this:

 (:---------------------------------------------------------------------------------------:) 
 string-join(
   let $in := .
   for $owner in distinct-values(//ContainerDataList/@EIGT)
      [let $o := . return count(distinct-values($in//ContainerDataList[@EIGT eq $o]/@CONR)) le 2]
   let $nrs := distinct-values(//ContainerDataList[@EIGT eq $owner]/@CONR)
   order by $owner 
   return (
     concat($owner, "  (", count($nrs), ")"),
     for $n in $nrs return concat("       ", $n)
   )
, "
")
(:---------------------------------------------------------------------------------------:) 

which is horrible, isn't it? The alternative would be to introduce an artificial for clause:

(:---------------------------------------------------------------------------------------:) 
  string-join(
   for $owner in distinct-values(//ContainerDataList/@EIGT)
   let $nrs := distinct-values(//ContainerDataList[@EIGT eq $owner]/@CONR)
   for $dummy in 1[count($nrs) le 1]
   return (
     concat($owner, "  (", count($nrs), ")"),
     for $n in $nrs return concat("       ", $n)
   )
, "
")
(:---------------------------------------------------------------------------------------:) 


If anyone considers this an acceptable alternative to a where clause, I give up.

With kind regards -
Hans-Juergen Rennau

> Message: 2
>Date: Tue, 9 Sep 2008 21:01:36 -0400
>  From: Robert Koberg <rob at koberg.com>
> Subject: [xquery-talk] FLOR or FLWOR
> To: talk at x-query.com
> Message-ID: <889FFFD6-3BBB-435A-A9B3-679797354799 at koberg.com>
> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
>
> Hi,
> 
> Where would you use 'where' that couldn't be handled by XPath? In  
> other words, why is there a where?
>
> best,
> -Rob
***********************************


__________________________________________________
Do You Yahoo!?
Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. 
http://mail.yahoo.com 



More information about the talk mailing list