简体   繁体   English

从变量分配xml元素名称

[英]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. 在将XPath表达式分配给XML元素内的属性时,必须用大括号将XPath表达式括起来,以指示XSLT处理器必须动态计算该值。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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