Re: [xsl] Trying to understand root-less or document-node-less nodes

Subject: Re: [xsl] Trying to understand root-less or document-node-less nodes
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 17 Sep 2007 13:43:36 +0100
On 9/17/07, Abel Braaksma <> wrote:
> Andrew Welch wrote:
> > What you're describing is the same as typing the variable as="element()" eg:
> >
> > <xsl:variable name="root" as="element()+">
> >   <test1 />
> >   <test2 />
> > </xsl:variable>
> >
> > By adding as="element()+" the variable goes from a root with two child
> > elements that are siblings (eg to access <test1/> use $root/test1) to
> > a sequence of elements with no root and that aren't siblings (to
> > access <test1/> use $root[1])
> Hi David, thanks for your response.

It was from me Abel... :)

> Surely that adds to clarity (and
> iirc, Michael Kay often tried to emphasize the use of the 'as' clause to
> prevent the document node from being created).

It's important to realise what happens when you add as="element()+" to
a variable definition - I've been caught out by it before when typing
all of my variables.

If you really need a document node because you need to maintain the
sibling relationships and you want to use the as attribute then you
need to use xsl:document eg:

<xsl:variable name="root" as="document-node()">

which is effectively the same as not using the as attribute:

<xsl:variable name="root">
</xsl:variable> in this case I decided that the "as" attribute can be left off
(this is the exception to the rule of "always use the as attribute").

The start of the thread where DC explains it is here:

> But root() never seems to
> fail: in your example, that means that the result is a sequence of two
> elements with each having a root equal to self::node()? And both, I
> guess, not being siblings from one another?

Yes, that sounds right.

Andrew Welch

Current Thread