2017-04-07 134 views
0

我已經瀏覽了所有關於XSLT排序的現有文章,但仍然無法找出適合我的排序情況的決定。 我需要先排序子節點(降序),然後根據第一個(最大)子值對父節點(降序)進行排序。XSLT根據最大子節點對父節點進行排序

所以,我需要最終訂單NAME3,1,名稱,但我有名稱1,名稱3,名稱2

請幫忙找一個解決方案。提前致謝!

輸入XML:

<collection> 
<products> 
    <product> 
     <productCode>001</productCode> 
     <productName>Name1</productName> 
     <subProducts> 
      <subProduct> 
       <prices> 
        <price> 
         <totalPrice>264.28</totalPrice> 
        </price>      
       </prices> 
      </subProduct> 
      <subProduct> 
       <prices> 
        <price> 
         <totalPrice>264.28</totalPrice> 
        </price> 
       </prices> 
      </subProduct>        
     </subProducts> 
    </product> 
    <product> 
     <productCode>002</productCode> 
     <productName>Name2</productName> 
     <subProducts> 
      <subProduct> 
       <prices> 
        <price> 
         <totalPrice>231.99</totalPrice> 
        </price> 
        <price> 
         <totalPrice>231.99</totalPrice> 
        </price> 
       </prices> 
      </subProduct> 
      <subProduct> 
       <prices> 
        <price> 
         <totalPrice>231.99</totalPrice> 
        </price> 
        <price> 
         <totalPrice>231.99</totalPrice> 
        </price> 
       </prices> 
      </subProduct>       
     </subProducts> 
    </product> 
    <product> 
     <productCode>003</productCode> 
     <productName>Name3</productName> 
     <subProducts> 
      <subProduct> 
       <prices> 
        <price> 
         <totalPrice>234.92</totalPrice> 
        </price> 
       </prices> 
      </subProduct> 
      <subProduct> 
       <prices> 
        <price> 
         <totalPrice>734.12</totalPrice> 
        </price>      
       </prices> 
      </subProduct>       
     </subProducts> 
    </product>   
</products> 
</collection> 

輸出XML:(預期)

<products> 
<product> 
    <productName>Name3</productName> 
    <price>734.12</price> 
    <price>234.92</price> 
</product> 
<product> 
    <productName>Name1</productName> 
    <price>264.28</price> 
    <price>264.28</price> 
</product> 
<product> 
    <productName>Name2</productName> 
    <price>231.99</price> 
    <price>231.99</price> 
    <price>231.99</price> 
    <price>231.99</price> 
</product> 
</products> 

XSLT轉換:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <xsl:copy> 
     <xsl:apply-templates select="/collection/products/product"> 
      <xsl:sort select="subProducts/subProduct[1]/prices/price[1]/totalPrice" data-type="number" order="descending"/>    
     </xsl:apply-templates>   
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/collection/products/product">  
    <xsl:copy>   
     <productName> 
      <xsl:value-of select="productName"/> 
     </productName>   
     <xsl:apply-templates select="subProducts/subProduct/prices/price"> 
      <xsl:sort select="totalPrice" order="descending" data-type="number"/> 
     </xsl:apply-templates>   
    </xsl:copy> 
</xsl:template> 

<xsl:template match="subProducts/subProduct/prices/price"> 
    <xsl:copy> 
     <xsl:value-of select="totalPrice"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

錯誤XML輸出:

<products> 
<product> 
    <productName>Name1</productName> 
    <price>264.28</price> 
    <price>264.28</price> 
</product> 
<product> 
    <productName>Name3</productName> 
    <price>734.12</price> 
    <price>234.92</price> 
</product> 
<product> 
    <productName>Name2</productName> 
    <price>231.99</price> 
    <price>231.99</price> 
    <price>231.99</price> 
    <price>231.99</price> 
</product> 
</products> 
+0

您將使用哪種XSLT處理器?在純粹的XSLT 1.0中,沒有擴展支持,你必須在兩遍中完成。 –

+0

@ michael.hor257k我有機會使用Xalan – MsPineapple

回答

0

如果您正在使用Xalan的處理器,你可以採取EXSLT math:max() extension function:

XSLT的優點1.0

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

<xsl:template match="/"> 
    <products> 
     <xsl:apply-templates select="collection/products/product"> 
      <xsl:sort select="math:max(subProducts/subProduct/prices/price/totalPrice)" data-type="number" order="descending"/>    
     </xsl:apply-templates>   
    </products> 
</xsl:template> 

<xsl:template match="product">  
    <xsl:copy>   
     <xsl:copy-of select="productName"/> 
     <xsl:apply-templates select="subProducts/subProduct/prices/price"> 
      <xsl:sort select="totalPrice" data-type="number" order="descending"/> 
     </xsl:apply-templates>   
    </xsl:copy> 
</xsl:template> 

<xsl:template match="price"> 
    <xsl:copy> 
     <xsl:value-of select="totalPrice"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

演示:http://xsltransform.net/3Mvmrzh/1

+0

非常感謝您的幫助!我已經在我的代碼中成功測試過了。 – MsPineapple