重命名结果元素的副本

时间:2014-05-14 16:35:36

标签: xml xslt

我需要的是更改xsl:copy-of产生的元素的名称。 我现在正在做的是:

base xml:

<root>
  <element>
     <type>type</type>
     <value id="id">
        <first>first value</first>
        <under id="id_under"/>
     </value>
  </element>
</root>

where元素可以是一个集合,value也可以是元素的集合。

我的xslt:

<xsl:for-each select="root/element">
    <xsl:for-each select="value">
       <xsl:copy-of select="current()" />
    </xsl:for-each>
</xsl:for-each>

有了这个,我得到以下结构:

..
    <value id="id">
       <first>first value</first>
       <under id="id_under"/>
    </value>
..

这是一系列价值观,而且确实如此,这正是我所需要的。我唯一需要转变的是&#34;值&#34;标记名称,并将其重命名为我已存储的变量。我怎样才能做到这一点?如果没有办法重命名&#34;输出&#34;复制的元素,我想唯一的方法是在模板中创建使用xsl:element和属性设置的身份来获取子元素?我对吗? 感谢。

1 个答案:

答案 0 :(得分:1)

如果您希望将元素调用为其他内容,那么您真的不需要copy-of它,是吗?

<xsl:for-each select="value">
  <xsl:element name="$your-variable">
    <xsl:copy-of select="@*" /> <!-- copy the attributes -->
    <xsl:copy-of select="*" />  <!-- copy the child elements -->
  </xsl:element>
</xsl:for-each>