Page 1 of 1

How can we generate dynamic ID using XQuery

Posted: Tue Jun 06, 2006 5:08 pm
by navanee
Hi Everybody!

Could you please let me know how can I generate dynamic IDs using XQuery (like generate-id() function in XSLT).

Thanks,

Regards,
Navanee.

Posted: Wed Jun 07, 2006 3:44 pm
by sorin_ristache
Hello,

XQuery does not provide an equivalent of generate-id() from XSLT. You can construct an ID based on the index of each of the ancestors of a given node in the list with all the siblings of that ancestor, as in the id function below:

Code: Select all

declare function local:pos($x) { 
let $parent := $x/..
for $child at $p in $parent/*
return (if ($child is $x) then $p else ())
};

declare function local:id($x) {
string-join(for $n in ($x/ancestor::node(),$x) return string(local:pos($n)), "/")
};


<test>
{
for $node in //*
return
<nameAndId>
{ concat(name($node), " --- ", local:id($node) ) }
</nameAndId>
}
</test>
Regards,
Sorin