Search code examples
xmlxsltxslt-1.0

xsl:element does not want to accept strings coming from an attribute value


I am trying to convert an element attribute value to a tag (name). Thus, for example, an <test name="testname"/> to a <testname/>. Mysteriously, it simply can not happen, I get the most various errors, among them

xsl:element: The value '$test' of the attribute 'name' is not a valid QName.

is the most important.

Minimal example, xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8"/>
  <xsl:template match="/">
    <xsl:variable name="test">
      <xsl:value-of select="/test/@name"/>
    </xsl:variable>
    <xsl:element name="$test"/>
  </xsl:template>
</xsl:stylesheet>

I am trying to execute it on this test xml:

<test name="testname"/>

Why? I can do the same without any problem if $test is a hardcoded string. What is this "qname" thing and how can I generate it from an attribute value?

I am using xslt-1.0 with the xsltproc tool, I am considering on an upgrade (probably to xalan or more likely, saxon he). But I would like to solve this problem yet within xsltproc.


Solution

  • Use an attribute value template <xsl:element name="{$varname}"> or more general <xsl:element name="{xpath-expression}">.