使用XSLT将XML元素转换为XML属性

时间:2022-05-25 20:33:02

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

我们现有的系统输出XML文件的格式如下:

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

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:

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

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

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

即项目的子节点需要转换为项目节点的属性。

Does someone know how this can be done using XSLT?

有人知道如何使用XSLT实现这一点吗?

5 个解决方案

#1


28  

That should work:

这应该工作:

<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

HTH

#2


4  

Here is probably the simplest solution that will convert any children-elements of ITEM to its attributes and will reproduce everything else as is, while converting the element names to any desired attribute names:

这里可能是最简单的解决方案,它可以将任何子元素转换为它的属性,并在将元素名称转换为任何期望的属性名称的同时复制其他所有内容:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
<!--                                              --> 
  <xsl:strip-space elements="*"/>

  <xsl:variable name="vrtfNameMapping">
    <item name="SERIALNUMBER" newName="serialNumber"/>
    <item name="LOCATION" newName="location"/>
    <item name="BARCODE" newName="barcode"/>
  </xsl:variable>
 <!--                                              --> 
  <xsl:variable name="vNameMapping" select=
  "document('')/*/xsl:variable[@name='vrtfNameMapping']"/>
<!--                                              --> 

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
<!--                                              --> 
  <xsl:template match="ITEM/*">
    <xsl:attribute name=
     "{$vNameMapping/*[@name=name(current())]/@newName}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

when the above transformation is applied on the provided XML document:

当上述转换应用于所提供的XML文档时:

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

the wanted result is produced:

所需结果如下:

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

Do note the following:

请注意以下:

  1. The use of the identity rule

    身份规则的使用

  2. The use of <xsl:strip-space elements="*"/>

    使用

  3. The use of the variable vrtfNameMapping without any xxx:node-set() extension function.

    变量vrtfNameMapping的使用没有任何xxx:node-set()扩展函数。

  4. The fact that we handle any mapping between a name and a newName, not only simple lower-casing.

    我们处理名称和新名称之间的任何映射,而不仅仅是简单的下框。

#3


4  

These two templates should do it:-

这两个模板应该做:-

<xsl:template match="ITEM">
   <ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}" />
</xsl:template>

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

#4


2  

This ought to do it:

应该这样做:

  <xsl:for-each select="//ITEM">
    <xsl:element name="ITEM">
      <xsl:attribute name="serialNumber">
        <xsl:value-of select="SERIALNUMBER"/>
      </xsl:attribute>
      <xsl:attribute name="location">
        <xsl:value-of select="LOCATION"/>
      </xsl:attribute>
      <xsl:attribute name="barcode">
        <xsl:value-of select="BARCODE"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:for-each>

Or using David's shortcut:

或者使用大卫的快捷方式:

<xsl:for-each select="//ITEM">
  <ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}"/>
</xsl:for-each>

#5


2  

If your source looks like this:

如果你的消息来源是这样的:

<row><a>1</a><b>2</b></row>

and you want it to look like this:

你希望它是这样的:

<row a="1" b="2" />

then this XSLT should work:

那么这个XSLT应该可以工作:

<xsl:template match="row">
    <row a="{a}" b="{b}" />
</xsl:template>

#1


28  

That should work:

这应该工作:

<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

HTH

#2


4  

Here is probably the simplest solution that will convert any children-elements of ITEM to its attributes and will reproduce everything else as is, while converting the element names to any desired attribute names:

这里可能是最简单的解决方案,它可以将任何子元素转换为它的属性,并在将元素名称转换为任何期望的属性名称的同时复制其他所有内容:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
<!--                                              --> 
  <xsl:strip-space elements="*"/>

  <xsl:variable name="vrtfNameMapping">
    <item name="SERIALNUMBER" newName="serialNumber"/>
    <item name="LOCATION" newName="location"/>
    <item name="BARCODE" newName="barcode"/>
  </xsl:variable>
 <!--                                              --> 
  <xsl:variable name="vNameMapping" select=
  "document('')/*/xsl:variable[@name='vrtfNameMapping']"/>
<!--                                              --> 

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
<!--                                              --> 
  <xsl:template match="ITEM/*">
    <xsl:attribute name=
     "{$vNameMapping/*[@name=name(current())]/@newName}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

when the above transformation is applied on the provided XML document:

当上述转换应用于所提供的XML文档时:

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

the wanted result is produced:

所需结果如下:

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

Do note the following:

请注意以下:

  1. The use of the identity rule

    身份规则的使用

  2. The use of <xsl:strip-space elements="*"/>

    使用

  3. The use of the variable vrtfNameMapping without any xxx:node-set() extension function.

    变量vrtfNameMapping的使用没有任何xxx:node-set()扩展函数。

  4. The fact that we handle any mapping between a name and a newName, not only simple lower-casing.

    我们处理名称和新名称之间的任何映射,而不仅仅是简单的下框。

#3


4  

These two templates should do it:-

这两个模板应该做:-

<xsl:template match="ITEM">
   <ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}" />
</xsl:template>

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

#4


2  

This ought to do it:

应该这样做:

  <xsl:for-each select="//ITEM">
    <xsl:element name="ITEM">
      <xsl:attribute name="serialNumber">
        <xsl:value-of select="SERIALNUMBER"/>
      </xsl:attribute>
      <xsl:attribute name="location">
        <xsl:value-of select="LOCATION"/>
      </xsl:attribute>
      <xsl:attribute name="barcode">
        <xsl:value-of select="BARCODE"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:for-each>

Or using David's shortcut:

或者使用大卫的快捷方式:

<xsl:for-each select="//ITEM">
  <ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}"/>
</xsl:for-each>

#5


2  

If your source looks like this:

如果你的消息来源是这样的:

<row><a>1</a><b>2</b></row>

and you want it to look like this:

你希望它是这样的:

<row a="1" b="2" />

then this XSLT should work:

那么这个XSLT应该可以工作:

<xsl:template match="row">
    <row a="{a}" b="{b}" />
</xsl:template>