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

Re: how to write such xquery?

From: Per Bothner <per@bothner.com>
Date: Tue, 09 Mar 2004 17:39:09 -0800
Message-ID: <404E71BD.7090408@bothner.com>
To: Chen Yabing <iscp0054@nus.edu.sg>
Cc: www-ql@w3.org

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 GMT

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