使用 XSLT 将 XML 元素转换为 XML 属性 [英] Converting XML elements to XML attributes using XSLT

查看:41
本文介绍了使用 XSLT 将 XML 元素转换为 XML 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个当前的系统,它输出一个 XML 文件,其格式如下:

We have a current system that outputs an XML file which is in the following format:

<INVENTORY>
   <ITEM>
      <SERIALNUMBER>something</SERIALNUMBER>
      <LOCATION>something</LOCATION>
      <BARCODE>something</BARCODE>
   </ITEM>
</INVENTORY>

我需要使用这些数据加载到标准 .NET 2.0 网格中.但是网格需要采用以下格式的 XML:

I need to use this data to load into the standard .NET 2.0 grid. But the grid needs the XML to be in the following format:

<INVENTORY>
   <ITEM serialNumber="something" location="something" barcode="something">
   </ITEM>
</INVENTORY>

即item的子节点需要转换为item节点的属性.

i.e. the child nodes of item need to be converted into attributes of the item node.

有人知道如何使用 XSLT 做到这一点吗?

Does someone know how this can be done using XSLT?

推荐答案

那应该可行:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="INVENTORY">
    <INVENTORY>
      <xsl:apply-templates/>
    </INVENTORY>
  </xsl:template>

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

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

HTH

这篇关于使用 XSLT 将 XML 元素转换为 XML 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆