简体   繁体   中英

Assign xml element name from variable

I would like to create a template with a param, and using this param create an element with that name.

How can this be done?

This is a non-working example, but gives an idea of what I want to do.

<xsl:call-template name="parseOneValue">
   <xsl:with-param name="name" select="'addr'" />
</xsl:call-template>

<xsl:template name="parseOneValue">
   <xsl:param name="name"/>
   <xsl:element name="$name">
     <xsl:attribute name="at">local
     </xsl:attribute>
   </xsl:element>
</xsl:template>

When assigning XPath expressions to attributes within an XML element you have to enclose the XPath expression with curly brackets to indicate to the XSLT processor that the value has to be dynamically calculated.

So the correct template would be:

<xsl:template name="parseOneValue">
   <xsl:param name="name"/>
   <xsl:element name="{$name}">
     <xsl:attribute name="at">local
     </xsl:attribute>
   </xsl:element>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM