0

我有一个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>我已经尝试使用and进行一些转换<xsl:apply-templates>,但对我没有任何效果。

提前致谢。

4

2 回答 2

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>
于 2013-04-19T10:59:13.400 回答
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>
于 2013-04-19T09:32:26.007 回答