XML 转换 - 将来自 "subnode" 的元素作为属性包含在父项中

XML Transformation - Include element from "subnode" in parent as attribute

我正在尝试通过 XSLT 转换 XML 文件。 输出 XML 需要包含元素作为属性。

我的输入文件如下所示:

<queryResponse>
 <entity>
  <DevicesDTO>
    <name>value</name>
    <type>value</type>
    <manufacturersNrs>
     <manufacturersNr>value</manufacturersNr>
    </manufacturersNrs>
  </DevicesDTO>
 </entity>
</queryResponse>

我的输出需要是这样的:

<Data>
 <Device name=xxx type=xxxx manufacturersNr=xxx />
</Data>

到目前为止,我已成功转换 xml 并将名称和类型作为属性。 但是 manufacturersNr 包含在 manufacturersNrs 中,我不知道如何将它包含在它上面的节点中。

我的 xslt 文件目前看起来像这样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="queryResponse">
    <Data>
      <xsl:apply-templates/>
    </Data>
  </xsl:template>

  <xsl:template match="devicesDTO">
    <Device>
      <xsl:for-each select="*">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="text()"/>
        </xsl:attribute>

      </xsl:for-each>
    </Device>
  </xsl:template>
</xsl:stylesheet>

对完整的 xslt 新手有任何提示吗? 我现在拥有的是我从各种来源拼凑而成的,所以如果我需要完全重写..那就这样吧。

如果您将 <xsl:for-each select="*"> 更改为 <xsl:for-each select="descendant::*[not(*)]">,我认为您可以轻松解决问题。