鉴于以下 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>