XSLT: Using attribute value templates in xsl:element

  • Thread starter Filip Hendrickx
  • Start date
F

Filip Hendrickx

Hi there.

I want to generate elements, choosing the element name dynamically. So I
tried to use attribute value templates:

<xsl:element name="{$local-name($someNode)}">
<!-- Generate element content. -->
</xsl:element>

where $someNode is an XML tree.

This doesn't work: the xslt processor sees the attribute value template
as an illegal qname. However, when I print out this value using
xsl:value-of, it appears to be perfectly valid.

So I changed the first line into

<xsl:element name="p{$local-name($someNode)}">

(notice the extra 'p' before the avt)
This works (strangely enough to me), but is of course useless as I don't
want that extra 'p'. I get the same behavior in Saxon b8.6.1, Xalan J
2.7.0 and Msxsl 4.0.

I know there are work-arounds to get the result that I want, but does
anybody know why the first version doesn't work and the second does?

Thanks in advance.

Filip Hendrickx.
 
G

George Bina

Hi Filip,

You have an extra $ in your AVT thus making the processor look for a
variable or parameter with the name local while what you want is the
local-name function. Here it is a sample stylesheet with that corrected
that works ok:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="someNode" select="/*"/>
<xsl:template match="/">
<xsl:element name="{local-name($someNode)}">
<!-- Generate element content. -->
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Best Regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top