RE: how to prune subtrees in an XQuery result

Here's a solution that doesn't involve doing the comparisons twice.

<Results> {
   for $A in doc("data.xml")/ASet/A
   let $matches := $A/(B[.=("red","yellow")]
                     |C[.=("chair","table")])
   where ($matches)
   return <A>{$matches}</A>
}</Results>

Michael Kay
http://www.saxonica.com/

> 
> Hi Sergio,
>   Probably this XQuery is required..
> 
> <Results>
> {
>       for $A in doc("data.xml")/ASet/A
>       where ($A/B = "red" or $A/B = "yellow")
>             and ($A/C = "chair" or $A/C = "table")    
>  
>       return 
>       <A>        
>         {$A/@*}
>         {$A/B[(. = 'red') or (. = 'yellow')]}
>         {$A/C[(. = 'chair') or (. = 'table')]}
>       </A>
> }
> </Results>
> 
> Regards,
> Mukul
> 
> --- Sergio Andreozzi <sergio.andreozzi@cnaf.infn.it>
> wrote:
> > 
> > Dear all,
> > 
> > I'm wondering what is the best way to prune subtrees
> > that did not match 
> > the where clause of a FLOWER expression.  See the
> > example below for 
> > clarification:
> > 
> > SAMPLE DATA
> > 
> > <ASet>
> > 	<A name="one">
> > 		<B>red</B>
> > 		<B>yellow</B>
> > 		<B>green</B>
> > 		<C>chair</C>
> > 		<C>table</C>
> > 		<C>sofa</C>
> > 	</A>
> > 	<A name="two">
> > 		<B>red</B>
> > 		<B>green</B>
> > 		<C>chair</C>
> > 		<C>sofa</C>
> > 	</A>
> > 	<A name="three">
> > 		<B>green</B>
> > 		<C>chair</C>
> > 		<C>table</C>
> > 		<C>sofa</C>
> > 	</A>
> > </ASet>
> > 
> > QUERY: for each A, list all the name of A, their B
> > elements that are 
> > either "red" or "yellow" and their C elements that
> > are either "chair" or 
> > "table"
> > 
> > EXPECTED RESULT:
> > 
> > <Results>
> > 	<A name="one">
> > 		<B>red</B>
> > 		<B>yellow</B>
> > 		<C>chair</C>
> > 		<C>table</C>
> > 	</A>
> > 	<A name="two">
> > 		<B>red</B>
> > 		<C>chair</C>
> > 	</A>
> > </Results>
> > 
> > 
> > in practice what I would like to understand is how
> > to prune all the 
> > subtrees that do not contain any match with
> > components of the WHERE 
> > clause in the XQuery result.
> > 
> > The general pruning rule is "if an element does not
> > participate in the 
> > satisfaction of the WHERE clause and all its
> > children elements (if any) 
> > don't participate as well, then prune it from the
> > result".
> > 
> > A possible starting point is the following query,
> > but the pruning action 
> > is missing.
> > 
> > <Results>
> > {
> >       for $A in doc("data.xml")/ASet/A
> >       where ($A/B = "red" or $A/B = "yellow")
> >             and ($A/C = "chair" or $A/C = "table")
> >       return ...
> > }
> > </Results>
> > 
> > 
> > 
> > Thanks,
> > 
> > 	Sergio
> 
> 
> 
> 		
> __________________________________ 
> Do you Yahoo!? 
> Yahoo! Small Business - Try our new resources site!
> http://smallbusiness.yahoo.com/resources/ 
> 
> 

Received on Sunday, 20 March 2005 19:16:42 UTC