RE: how to write such xquery?

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 UTC