How can we generate dynamic ID using XQuery

Here should go questions about transforming XML with XSLT and FOP.
navanee
Posts: 1
Joined: Tue Jun 06, 2006 5:04 pm

How can we generate dynamic ID using XQuery

Post 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.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post 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
Post Reply