0

次の XML を考えると、関連する親ノード内<OrderDate>の各ノードの子にノードを移動/コピーする機能を xsl ファイルに追加したいと思います。したがって、基本的には、すでに 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に適用すると(これはあなたの例に基づいていますが、並べ替えがより明確になるように日付の1つと注文の順序を変更しました):

<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 に答える