Re: [xsl] xsl:attributes as parameter to a template

Subject: Re: [xsl] xsl:attributes as parameter to a template
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 3 May 2005 19:53:17 +1000
On 5/3/05, Jan Tietjens <jan.tietjens@xxxxxxxxxxxxxx> wrote:
> Hello,
> I am not understanding why the following xslt snipped produces an empty
> 'someElement' element (XSLT 1.0):
>
>    <xsl:template match="/">
>        <xsl:call-template name="printSomeAttributes">
>            <xsl:with-param name="attributes">
>                <xsl:attribute name="juhu">Hello World!</xsl:attribute>
>            </xsl:with-param>
>        </xsl:call-template>
>    </xsl:template>
>
>    <xsl:template name="printSomeAttributes">
>        <xsl:param name="attributes"/>
>        <xsl:element name="someElement">
>            <xsl:value-of select="$attributes"/>
>        </xsl:element>
>    </xsl:template>
>


If you run it with Saxon 8.4 the error message that is produced
describes the cause of the problem very well:

   "Error at attribute constructor juhu="{...}" on line 6 of
file:/C:/Program%20Files/Java/jdk1.5.0_01/bin/marrowtr.xsl:
  XTDE0410: Cannot create an attribute node whose parent is a document node
Transformation failed: Run-time errors were reported"


Saxon 6.5.3 produces also a good message:

"Recoverable error
at xsl:attribute on line 6 of file:/(Untitled):
  Cannot write an attribute node when no element start tag is open"

What is happening is that the previous node produced is not an element
(in fact it is a root node), so the attribute (even if created) cannot
be attached to an element node.

The result is either an error (in XSLT 2.0) or a recoverable error (as
in XSLT 1.0) in which case no attribute is present in the output.


Cheers,
Dimitre Novatchev

Current Thread