[xquery-talk] Using axis in XQuery

Michael Kay mhk at mhk.me.uk
Wed Dec 1 09:19:46 PST 2004

> I need a function such that : For every node returned as an answer to
> an xquery which is a union of xqueries, I want the path from the root
> to that node in the document.
> <xsl:for-each select="ancestor-or-self::*">
>   <xsl:text>/</xsl:text>
>   <xsl:value-of select="name()" />
> </xsl:for-each>

This can actually be simplified in the latest draft of XPath 2.0 to the

concat('/', string-join(ancestor-or-self::*/name(), '/'))


(a) the ability to have the rh operand of "/" return atomic values is very
new in the latest draft, in most current products you will probably need to
write it as

concat('/', string-join(for $n in ancestor-or-self::* return name($n), '/'))

(b) the XQuery spec makes the ancestor axis optional. This (in my view) is a
real pain, because it means you have to implement it yourself using a
recursive function. I expect all XQuery vendors will be forced by user
pressure to implement the ancestor axis eventually, but some of them are
very resistant, apparently because recursive queries are difficult to

Michael Kay

More information about the talk mailing list