Re: [xsl] What does name="{name()}" mean?

Subject: Re: [xsl] What does name="{name()}" mean?
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Fri, 25 Dec 2009 12:58:15 -0500
> Sometimes in templates a pattern like the following appear:
> <xsl:template .....>
>   <xsl:element name="{name()}" ....> 
>   ....
>   </xsl:element>
> </xsl:template>
> What do the curly brackets and name="{name()}" mean?

Hmm... other people who frequent this list are far more qualified to
provide a precise answer than I, but I can give you a quick idea.

The <xsl:element> element puts an element node onto the output tree.
The name of the element it puts on the output tree is specified by
the value of its name= attribute. So
    <xsl:element name="platypus">
      <xsl:attribute name="bill">duck</xsl:attribute>
   <platypus bill="duck">Prius</platypus>
into the output.

In XSLT, some attributes take as their value an "attribute value
template". The values of these attributes are "an alternating
sequence of fixed parts and variable parts. A variable part consists
of an XPath expression enclosed in curly brackets ({}). A fixed part
may contain any characters, except that a left curly bracket must be
written as {{ and a right curly bracket must be written as }}."

So the curly braces mean "evaluate my contents as an XPath
expression". The expression, here, consists of a call to the name()
function, which returns the fully qualified name (i.e., both the
namespace part and the local part) of the name of the node that is
passed to name(), or (as is the case here) by default the context
node (i.e., name() is the same as name(.)).

I think this has pretty much the same net effect as
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
which I prefer only because it is more explicit.

Current Thread