Re: how to write such xquery?

Chen Yabing wrote:

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

You can use parent context explicitly (e.g. the parent:: axis), or
implicitly, using recursion.  The latter is probably more efficient.
Whether it's easier I don't know; I show a solution using recursion
below.

> 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.

This is still not very clear, and it's a rather weird problem.  However.
this solution gives the desired result.

declare function convert-children ($x) {
  for $y in $x/node() return convert-node($y)
};
declare function strip-children ($x) {
  for $y in $x/node()
  where ($y[@dno="d002"] or not($y instance of element(d,*)))
  return $y
};
declare function convert-node($x) {
   if ($x instance of element() and $x/d[@dno="d002"])
     then element {node-name($x)} {$x/@*, strip-children($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-node())
     then document {convert-children($x)}
   else ()
};
convert-node(doc("/tmp/dd.xml"))

yields (using the CVS version of Qexo) the desired result.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

Received on Tuesday, 9 March 2004 20:39:14 UTC