XSLT 连接元素名称和属性值

XSLT to concatenate element name with attribute value

我怎么能把 XML 变成这样:

<nameGroup index="1"> 
    <name>SOME NAME</name>
    <country>CN</country>
</nameGroup>
<nameGroup index="2">
    <name>SOME OTHER NAME</name>
    <country>IQ</country>
</nameGroup>

对于这样的 XML,将元素名称与属性值连接起来:

<nameGroup><name1>SOME NAME</name1><country1>CN</country1></nameGroup>
<nameGroup><name2>SOME OTHER NAME</name2><country2>IQ</country2></nameGroup>

使用适当的 XSLT?

我试过这样的东西:

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

  <xsl:template match="/">
        <xsl:copy>
            <xsl:apply-templates select="*" />
        </xsl:copy>
    </xsl:template>
  
  <xsl:template match="nameGroup/name">
      <xsl:variable name="pos">
          <xsl:number count="info"/>
      </xsl:variable>
      <xsl:element name="{local-name()}{$pos}">
          <xsl:apply-templates/>
     </xsl:element>
   </xsl:template>
   
   <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

但是结果是这样的:

<nameGroup>1 
  <name>SOME NAME</name>
  <country>CN</country>
</nameGroup>
<nameGroup>2
  <name>SOME OTHER NAME</name>
  <country>IQ</country>
 </nameGroup>

有人知道我怎样才能得到想要的结果吗? 谢谢!

PS:我是这样弄的(如果以后有人搜索的话): (在 Michael Kay 先生的帮助下)

<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*">
        <xsl:element name="{local-name()}{../@index}">
            <xsl:apply-templates select="* | node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

我无法想象为什么会有人想要这样做;我也不明白你为什么觉得这很困难。你只需要规则

<xsl:template match="namegroup/*">
  <xsl:element name="{local-name()}{../@index}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>