如何使用xslt将嵌套的xml表转换为平面表

时间:2013-04-19 09:05:35

标签: xml xslt

我有一个xml文件

<xml>
  <order>
    <orderid>123</orderid>
    <orderdate>2013-04-12T00:00:00.000+01:00</orderdate>
    <orderline>
      <articlename>AAAA</articlename>
      <quantity>10</quantity>
    </orderline>
    <orderline>
      <articlename>BBBB</articlename>
      <quantity>15</quantity>
    </orderline>
  </order>
  <order>
    ...
    </order>
</xml>

我需要使用XSLT将其转换为:

<xml>
  <item>
    <orderid>123</orderid>
    <articlename>AAAA</articlename>
    <quantity>10</quantity>
  </item>
  <item>
    <orderid>123</orderid>
    <articlename>BBBB</articlename>
    <quantity>15</quantity>
  </item>
</xml>

我已经尝试使用<xsl:call-template><xsl:apply-templates>进行了一些转换,但对我来说没有任何效果。

提前致谢。

2 个答案:

答案 0 :(得分:2)

这个简单的样式表应该这样做:

样式表

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

  <!--
  Identity transform:
  http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT 
  -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="orderline">
    <item>
      <!--
      Apply preceding <orderid> sibling and the children of the current element
      -->
      <xsl:apply-templates select="preceding-sibling::orderid | node()"/>
    </item>
  </xsl:template>

  <xsl:template match="order">
    <!--
    Only apply <orderline> children, <orderdate> is dropped and <orderid> is
    handled by the template above
    -->
    <xsl:apply-templates select="orderline"/>
  </xsl:template>
</xsl:stylesheet>

输入

<xml>
  <order>
    <orderid>123</orderid>
    <orderdate>2013-04-12T00:00:00.000+01:00</orderdate>
    <orderline>
      <articlename>AAAA</articlename>
      <quantity>10</quantity>
    </orderline>
    <orderline>
      <articlename>BBBB</articlename>
      <quantity>15</quantity>
    </orderline>
  </order>

  <!-- Added additional <order> element for demonstration -->
  <order>
    <orderid>456</orderid>
    <orderdate>2014-05-13T00:00:00.000+02:00</orderdate>
    <orderline>
      <articlename>CCCC</articlename>
      <quantity>20</quantity>
    </orderline>
    <orderline>
      <articlename>DDDD</articlename>
      <quantity>25</quantity>
    </orderline>
  </order>
</xml>

输出

<?xml version="1.0" encoding="utf-8"?>
<xml>
  <item>
    <orderid>123</orderid>
    <articlename>AAAA</articlename>
    <quantity>10</quantity>
  </item>
  <item>
    <orderid>123</orderid>
    <articlename>BBBB</articlename>
    <quantity>15</quantity>
  </item>
  <item>
    <orderid>456</orderid>
    <articlename>CCCC</articlename>
    <quantity>20</quantity>
  </item>
  <item>
    <orderid>456</orderid>
    <articlename>DDDD</articlename>
    <quantity>25</quantity>
  </item>
</xml>

答案 1 :(得分:0)

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <xml>
         <xsl:for-each select="//order/orderline">
        <item>
           <orderid><xsl:value-of select="//orderid" /></orderid>
             <articlename><xsl:value-of select="articlename" /></articlename>
             <quantity><xsl:value-of select="quantity" /></quantity> 
         </item>
            </xsl:for-each>

  </xml>
</xsl:template>
</xsl:stylesheet>