0

鉴于以下 XML,我想向 xsl 文件添加将节点移动/复制到其关联父节点<OrderDate>中每个节点的子节点的能力。所以基本上它应该像在 XSL 中一样按日期排序并移动/复制。不知道如何做到这一点。我尝试过的所有尝试都接近但失败了。<OrderDetails><Orders>

启动 XML

<xmldata>
    <Orders>
        <OrderID>87709</OrderID>
        <CustomerID>123456</CustomerID>
        <OrderDate>10/30/2012 3:02:00 PM</OrderDate>
        <PaymentMethodID>5</PaymentMethodID>
        <PONum></PONum>
        <ShippingMethodID>502</ShippingMethodID>
        <OrderDetails>
            <OrderDetailID>25676</OrderDetailID>
            <OrderID>87709</OrderID>
            <ProductCode>58309-SYL</ProductCode>
            <ProductID>9756</ProductID>
            <ProductName>ABCD</ProductName>
            <ProductPrice>4.7500</ProductPrice>
            <Quantity>300</Quantity>
            <TotalPrice>1425.0000</TotalPrice>
        </OrderDetails>
        <OrderDetails>
            <OrderDetailID>25677</OrderDetailID>
            <OrderID>87709</OrderID>
            <ProductCode>ABCDEFG</ProductCode>
            <ProductID>9757</ProductID>
            <ProductName>XYZ 2</ProductName>
            <ProductPrice>4.7500</ProductPrice>
            <Quantity>300</Quantity>
            <TotalPrice>1425.0000</TotalPrice>
        </OrderDetails>
    </Orders>
    <Orders>
        <OrderID>84757</OrderID>
        <CustomerID>123456</CustomerID>
        <OrderDate>10/30/2012 3:02:00 PM</OrderDate>
        <PaymentMethodID>5</PaymentMethodID>
        <PONum></PONum>
        <ShippingMethodID>141</ShippingMethodID>
        <OrderDetails>
        <OrderDetailID>21472</OrderDetailID>
        <OrderID>84757</OrderID>
        <ProductCode>58309</ProductCode>
        <ProductID>9756</ProductID>
        <ProductName>XYZ 12V</ProductName>
        <ProductPrice>4.7500</ProductPrice>
        <Quantity>160</Quantity>
        <TotalPrice>760.0000</TotalPrice>
        </OrderDetails>
    </Orders>
    <Orders>
     next Order...
    </Orders>
</xmldata>

和以下 XSL

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
  <xmldata>

  <xsl:for-each select="//Orders">
    <xsl:sort select="substring-before(substring-after(substring-after(OrderDate,'/'),'/'),' ')" data-type="number" order="descending"/> <!-- year -->
    <xsl:sort select="substring-before(OrderDate,'/')" data-type="number"  order="descending"/> <!-- month -->
    <xsl:sort select="substring-before(substring-after(OrderDate,'/'),'/')" data-type="number" order="descending"/> <!-- day -->
    <xsl:copy-of select="."/>
 </xsl:for-each>
</xmldata>
</xsl:template>
</xsl:stylesheet>

转换后的 XML

<xmldata>
    <Orders>
        <OrderID>84757</OrderID>
        <CustomerID>123456</CustomerID>
        <PaymentMethodID>5</PaymentMethodID>
        <PONum></PONum>
        <ShippingMethodID>141</ShippingMethodID>
        <OrderDetails>
            <OrderDate>6/13/2013 5:08:00 PM</OrderDate>
            <OrderDetailID>21472</OrderDetailID>
            <OrderID>84757</OrderID>
            <ProductCode>58309</ProductCode>
            <ProductID>9756</ProductID>
            <ProductName>XYZ 12V</ProductName>
            <ProductPrice>4.7500</ProductPrice>
            <Quantity>160</Quantity>
            <TotalPrice>760.0000</TotalPrice>
        </OrderDetails>
    </Orders>
    <Orders>
        <OrderID>87709</OrderID>
         <CustomerID>123456</CustomerID>
        <PaymentMethodID>5</PaymentMethodID>
        <PONum></PONum>
        <ShippingMethodID>502</ShippingMethodID>
        <OrderDetails>
            <OrderDate>10/30/2012 3:02:00 PM</OrderDate>
            <OrderDetailID>25676</OrderDetailID>
            <OrderID>87709</OrderID>
            <ProductCode>58309-SYL</ProductCode>
            <ProductID>9756</ProductID>
            <ProductName>ABCD</ProductName>
            <ProductPrice>4.7500</ProductPrice>
            <Quantity>300</Quantity>
            <TotalPrice>1425.0000</TotalPrice>
         </OrderDetails>
        <OrderDetails>
            <OrderDate>10/30/2012 3:02:00 PM</OrderDate>
            <OrderDetailID>25677</OrderDetailID>
            <OrderID>87709</OrderID>
            <ProductCode>ABCDEFG</ProductCode>
            <ProductID>9757</ProductID>
            <ProductName>XYZ 2</ProductName>
            <ProductPrice>4.7500</ProductPrice>
            <Quantity>300</Quantity>
            <TotalPrice>1425.0000</TotalPrice>
        </OrderDetails>
    </Orders>
    <Orders>
    next Order...
    </Orders>
</xmldata>
4

1 回答 1

1

以下 XSLT 样式表:

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

  <!-- The identity transform. -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- When we reach the xmlData element, copy it and select its Order children by reverse date order. -->
  <xsl:template match="xmldata">
    <xsl:copy>
      <xsl:apply-templates select="Orders">
        <!-- year -->
        <xsl:sort select="substring-before(substring-after(substring-after(OrderDate,'/'),'/'),' ')"
                  data-type="number"
                  order="descending"/>
        <!-- month -->
        <xsl:sort select="substring-before(OrderDate,'/')"
                  data-type="number"
                  order="descending"/>
        <!-- day -->
        <xsl:sort select="substring-before(substring-after(OrderDate,'/'),'/')"
                  data-type="number"
                  order="descending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!-- Copy out the Orders element and its children, except for OrderDate. -->
  <xsl:template match="Orders">
    <xsl:copy>
      <xsl:apply-templates select="node()[not(self::OrderDate)]|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- Copy out the OrderDetails element and its children, adding the OrderDate for the Order. -->
  <xsl:template match="OrderDetails">
    <xsl:copy>
      <xsl:copy-of select="../OrderDate"/>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>            
  </xsl:template>

</xsl:stylesheet>

当应用于此输入 XML 时(基于您的示例,但我更改了订单的日期和顺序之一,以便排序更明显):

<xmldata>
  <Orders>
    <OrderID>84757</OrderID>
    <CustomerID>123456</CustomerID>
    <OrderDate>10/30/2012 3:02:00 PM</OrderDate>
    <PaymentMethodID>5</PaymentMethodID>
    <PONum></PONum>
    <ShippingMethodID>141</ShippingMethodID>
    <OrderDetails>
      <OrderDetailID>21472</OrderDetailID>
      <OrderID>84757</OrderID>
      <ProductCode>58309</ProductCode>
      <ProductID>9756</ProductID>
      <ProductName>XYZ 12V</ProductName>
      <ProductPrice>4.7500</ProductPrice>
      <Quantity>160</Quantity>
      <TotalPrice>760.0000</TotalPrice>
    </OrderDetails>
  </Orders>
  <Orders>
    <OrderID>87709</OrderID>
    <CustomerID>123456</CustomerID>
    <OrderDate>6/13/2013 5:08:00 PM</OrderDate>
    <PaymentMethodID>5</PaymentMethodID>
    <PONum></PONum>
    <ShippingMethodID>502</ShippingMethodID>
    <OrderDetails>
      <OrderDetailID>25676</OrderDetailID>
      <OrderID>87709</OrderID>
      <ProductCode>58309-SYL</ProductCode>
      <ProductID>9756</ProductID>
      <ProductName>ABCD</ProductName>
      <ProductPrice>4.7500</ProductPrice>
      <Quantity>300</Quantity>
      <TotalPrice>1425.0000</TotalPrice>
    </OrderDetails>
    <OrderDetails>
      <OrderDetailID>25677</OrderDetailID>
      <OrderID>87709</OrderID>
      <ProductCode>ABCDEFG</ProductCode>
      <ProductID>9757</ProductID>
      <ProductName>XYZ 2</ProductName>
      <ProductPrice>4.7500</ProductPrice>
      <Quantity>300</Quantity>
      <TotalPrice>1425.0000</TotalPrice>
    </OrderDetails>
  </Orders>
</xmldata>

生成以下输出 XML:

<xmldata>
  <Orders>
    <OrderID>87709</OrderID>
    <CustomerID>123456</CustomerID>
    <PaymentMethodID>5</PaymentMethodID>
    <PONum />
    <ShippingMethodID>502</ShippingMethodID>
    <OrderDetails>
      <OrderDate>6/13/2013 5:08:00 PM</OrderDate>
      <OrderDetailID>25676</OrderDetailID>
      <OrderID>87709</OrderID>
      <ProductCode>58309-SYL</ProductCode>
      <ProductID>9756</ProductID>
      <ProductName>ABCD</ProductName>
      <ProductPrice>4.7500</ProductPrice>
      <Quantity>300</Quantity>
      <TotalPrice>1425.0000</TotalPrice>
    </OrderDetails>
    <OrderDetails>
      <OrderDate>6/13/2013 5:08:00 PM</OrderDate>
      <OrderDetailID>25677</OrderDetailID>
      <OrderID>87709</OrderID>
      <ProductCode>ABCDEFG</ProductCode>
      <ProductID>9757</ProductID>
      <ProductName>XYZ 2</ProductName>
      <ProductPrice>4.7500</ProductPrice>
      <Quantity>300</Quantity>
      <TotalPrice>1425.0000</TotalPrice>
    </OrderDetails>
  </Orders>
  <Orders>
    <OrderID>84757</OrderID>
    <CustomerID>123456</CustomerID>
    <PaymentMethodID>5</PaymentMethodID>
    <PONum />
    <ShippingMethodID>141</ShippingMethodID>
    <OrderDetails>
      <OrderDate>10/30/2012 3:02:00 PM</OrderDate>
      <OrderDetailID>21472</OrderDetailID>
      <OrderID>84757</OrderID>
      <ProductCode>58309</ProductCode>
      <ProductID>9756</ProductID>
      <ProductName>XYZ 12V</ProductName>
      <ProductPrice>4.7500</ProductPrice>
      <Quantity>160</Quantity>
      <TotalPrice>760.0000</TotalPrice>
    </OrderDetails>
  </Orders>
</xmldata>
于 2013-09-26T12:43:47.027 回答