如何使用 XSLT 重命名 XML 元素及其第一个属性的值?

How do I rename an XML element with the value of its first attribute using XSLT?

我是 XSLT 新手,XML 结构如下所示:

  <Loop LoopId="1000A" Name="SUBMITTER NAME">
  .... a bunch of sub-elements, etc. 
  </Loop>

我正在尝试编写一个 XSLT,将它们全部转换为: (将 LoopId 属性的值连接到其父元素名称)

  <Loop1000A LoopId="1000A" Name="SUBMITTER NAME">
  .... a bunch of sub-elements, etc. 
  </Loop1000A>

我有一个样式 sheet 几乎可以让我到达那里,但它正在摆脱属性 LoopId 我不知道为什么 - 下面的样式 sheet 产生这个结果:

  <Loop1000A Name="SUBMITTER NAME">
  .... a bunch of sub-elements, etc. 
  </Loop1000A>

有没有办法修改它以便我保留 LoopId 属性?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@LoopId"/>

  <xsl:template match="*[@LoopId]">
     <xsl:variable name="vRep" select="concat('Loop',@LoopId)"/>
    <xsl:element name="{$vRep}">
      <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

谢谢

变化:

<xsl:element name="concat('Loop', @LoopId)">

至:

<xsl:element name="{concat('Loop', @LoopId)}">

参见: https://www.w3.org/TR/xslt/#attribute-value-templates

删除模板 <xsl:template match="@LoopId"/> 就像删除 LoopId 属性一样。