W3C home > Mailing lists > Public > www-ql@w3.org > January to March 2004

RE: how to write such xquery?

From: Chen Yabing <iscp0054@nus.edu.sg>
Date: Mon, 8 Mar 2004 11:19:53 +0800
Message-ID: <3630B143EA0D7246BDF7EF9FBDF6F9ABAB44B0@MBXSRV26.stu.nus.edu.sg>
To: "Per Bothner" <per@bothner.com>, "Xavier Franc" <xfranc@online.fr>
Cc: <www-ql@w3.org>
Per Bothner wrote:

 

>Chen Yabing's problem doesn't require parent context, so it

>can be simply expressed without "matching", though because

>he does need to match type attributes he can't just use

>typeswitch.  I'm not sure I understand the problem correctly,

>but would something like this work?

 

 

>declare function convert-children ($x) {

>  for $y in $x/node() return convert-node($y)

>};

 

>declare function convert-node($x) {

>   if ($x instance of element(d,*) and $x[@dno="d002"])

>     then $x

>   else if ($x instance of element() and $x//d[@dno="d002"])

>     then element {node-name($x)} {$x/@*, convert-children($x)}

>   else if ($x instance of document())

>     then document {convert-children($x)}

>   else () };

 

I am sorry that I did not present my problem clearly. Actually the problem requires parent context. 

 

Let’s say, if the query finds an element d which has only one instance with dno≠"d002", then the query should remove all parent and child instances of the instance of d. On the other hand, if the query finds an element d which has instance with dno="d002" and other instances, then only removes the other instances and keep the parent and child instances of the instance of d.

 

 

Say the source is 

<a ano=”a001”>

   <b bno=”b001”>

       <c cno=”c001”/>

       <d dno=”d001”/>

       <d dno=”d002”/>

   </b>

   <b bno=”b002”>

       <c cno=”c002”/>

       <d dno=”d001”/>

   </b>

</a>

 

Then the query result should be 

<a ano=”a001”>

   <b bno=”b001”>

       <c cno=”c001”/>

       <d dno=”d002”/>

   </b>   

</a>
Received on Sunday, 7 March 2004 22:20:15 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Saturday, 22 July 2006 00:10:19 GMT