Re: Combine sub elements in identical elements

Chen Yabing wrote:
> Hi,
> 
>  it seems that XQuery cannot place elements together even if they have 
> same parent elements in the result. I mean that the result will produce 
> two identical elements for each sub elements.

The following program:

for  $j in document("spj.xml")//project
return
   <project jno = {$j/@jno}>{
     for          $s in $j/supplier,
          $p in $s/part
where    $p/price >100
return
                     <supplier sno = {$s/@sno}>
                           <part pno = {$p/@pno}>
                                  {$p/price}
                           </part>
                     </supplier>
}
        </project>

run under Qexo (www.qexo.org) produces:

<project jno="j001"><supplier sno="s001">
                           <part pno="p001">
                                  <price>190</price>
                           </part>
                     </supplier><supplier sno="s002">
                           <part pno="p001">
                                  <price>110</price>
                           </part>
                     </supplier>
        </project><project jno="j002"><supplier sno="s002">
                           <part pno="p002">
                                  <price>120</price>
                           </part>
                     </supplier>
        </project>

One possible problem with this:  It still emits a <project>
element even if there are no matching parts or suppliers.
That could be fixed using a variable (use let) and testing
with empty.
-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/per/

Received on Monday, 18 February 2002 12:32:48 UTC