RE: Combine sub elements in identical elements

You basically have a join with your three FOR clauses.

 

In order to do what you want to do, you have to move some of the FOR
into the RETURN.

 

Best regards

Michael

 

-----Original Message-----
From: Chen Yabing [mailto:iscp0054@nus.edu.sg] 
Sent: Monday, February 18, 2002 4:21 AM
To: 'www-ql@w3.org'
Subject: Combine sub elements in identical elements

 

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.

For example, the source document: 
<db> 
        <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> 
</db> 

the query is: 

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

and the result is: 

<project jno="j001">    
    <supplier sno="s001">      
      <part pno="p001"> 
        <price>190</price> 
      </part> 
    </supplier> 
  </project> 
  <project jno="j001">    
    <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> 
</xql:result> 

In the ideal situation, the query result should be identical to the
source document. but there are two project whose jno is "j001". 

If I want to produce a result which will combine elements under the
identical parent elements together so that there are only one parent
elements. How can I do based on the above query?

Thank you! 

Received on Monday, 18 February 2002 12:50:27 UTC