Q:XQuery and 'outerjoin'

Could someone come up with a solution for the following problem (challenge :wink:

I do have the following document (instance of a specific schema)



mo
tu














Element Allocation is optional!

Now I would like to get all documents with and without allocations using XQuery4

The following XQuery only returns documents WITH allocation, but I do also want documents WITHOUT allocations!

How do I proceed. A union would be a solution but I have no idea how to accomplish this with XQuery4.

Here is the xquery that produces all documents with an allocation

--------------------------------
for $chkplan in input()/Chkplan

sort by ($chkplan/@fltnr)
return
for $alloc in $chkplan/Allocation {
{ $alloc/@rlendtime } { $alloc/@rlstarttime } { $chkplan/@fltnr }
{ $chkplan/Operationaldays }
{ for $row in $alloc/Checkin return
{ $row/@chkinrow }

}

}
--------------------------------

Regards,
Rudolf de Grijs

Hi Rudolf,

The above xml is not valid and xquery is not valid so could you zip up and post the following:
1) actual example document with allocations
2) actual example document without allocations
3) actual xquery you have working so far (that does just produces a result of documents with allocations)
4) and what you really expect to see in the result: i.e. for those documents without allocations what would you expect to see in the result set.

Bye for now,

Stuart Fyffe-Collins
Software AG (UK) Ltd.

Hi Stuart,

Ok, I may have made a typo. Here is the real thing:

I have two (valid) XML documents



mo
tu
th
su





















we





One document contains Chkalloc elements and teh last one does not.

Now I would like to ‘browse’ over the allocations (and it works, but the latter is left out in the final result)

The XQuery(4)

for $chkplan in input()/Chkplan

sort by ($chkplan/@fltnr)
return
for $alloc in $chkplan/Chkalloc
return
{ $alloc/@rlendtime } { $alloc/@rlstarttime } { $chkplan/@fltnr } { $chkplan/@protect }
{ $chkplan/Opdays }
{ for $row in $alloc/Checkin return
{ $row/@chkinrow }

}


What I also want, besides all documents with Chkalloc elements, are documents without Chkalloc elements, so
in that case the attributes { $alloc/@rlendtime } { $alloc/@rlstarttime } and the Row elements are missing in the final result.


I’m not able to accomplish this, but you would make me happy if you can :wink:

Kind regards,
Rudolf de Grijs

Hello Rudolf,

How about the following xquery. Seems to work for me.

  
for $chkplan in input()/Chkplan
sort by ($chkplan/@fltnr)
return (
  (let $e := for $alloc in $chkplan/Chkalloc
             return
                 <Allocation> { $alloc/@rlendtime } 
                              { $alloc/@rlstarttime } 
                              { $chkplan/@fltnr } 
                              { $chkplan/@protect } 
                              { $chkplan/Opdays } 
                              { for $row in $alloc/Checkin return 
                                   <Row> { $row/@chkinrow } </Row> 
                              } 
                 </Allocation> 
   where count($chkplan/Chkalloc) > 0 return $e),

  (let $e := <Allocation>
                              { $chkplan/@fltnr } 
                              { $chkplan/@protect } 
                              { $chkplan/Opdays } 
             </Allocation>
   where count($chkplan/Chkalloc) = 0 return $e)
)



Really hope this helps.

Stuart Fyffe-Collins
Software AG (UK) Ltd.

Thanks Stuart … it works ;-).


Kind regards,
Rudolf

Hi,
I am very new to both XML and XQuery, but I think I have a problem that could be solved using parts of the code given in the previous messages of this thread. I want to retrieve all XML documents that do not have a particular element. As I said, I am extremely new to this, so my apologies if my question is strangely worded or exceedingly simple.

An example of my schema is this:

2005 04001 55 Imaging of keratin dynamics during the cell cycle and in response to phosphatase inhibition. Windoffer R Leube RE Meth. Cell Biol. 23 321-352 A

What I am trying to do, is write a query that will give me all documents that do not contain a ‘Bewertung’ element.

I hope it’s a simple matter. In the meantime, I will continue trying. Thanks! :slight_smile:

Kathy

Hi Kathy,
A simple query like below should be able to do the job.

Finn
PS The query is only optimized if there is an standard index on “Bewertung” !!!

for $a in input()/Publikation
where not ($a/Bewertung)
return $a

Quick response!

Thanks a lot. :slight_smile: We got it working.

KT