[英]XSLT sort parent nodes based on maximum child node
我瀏覽了所有有關XSLT排序的現有文章,但仍無法為我的排序情況找到合適的決定。 我需要首先對子節點排序(降序),然后根據第一個(最大)子值對父節點排序(降序)。
因此,我需要最終訂單Name3,Name1,Name2 ,但我具有Name1,Name3,Name2 。
您能幫忙找到解決方案嗎? 提前致謝!
輸入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>
如果您使用的是Xalan處理器,則可以利用EXSLT math:max()擴展功能:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.