I. Có một XSLT nói chung và tinh khiết 1.0 giải pháp - đơn giản như này:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<products>
<xsl:apply-templates select="*">
<xsl:sort data-type="number" select=
"price/*[not(../* < .)]"/>
</xsl:apply-templates>
</products>
</xsl:template>
</xsl:stylesheet>
II.Nếu price
có các trẻ khác ngoài offer
và orig
- trong trường hợp này là giải pháp chung I. ở trên (cũng như hai câu trả lời khác cho câu hỏi này) không hoạt động chính xác.
Dưới đây là một giải pháp đúng cho trường hợp này:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<products>
<xsl:apply-templates select="*">
<xsl:sort data-type="number" select=
"sum(price/orig[not(../offer <= .)])
+
sum(price/offer[not(../orig < .)])
"/>
</xsl:apply-templates>
</products>
</xsl:template>
</xsl:stylesheet>
III. Nếu chúng ta biết rằng offer
không bao giờ vượt quá orig
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<products>
<xsl:apply-templates select="*">
<xsl:sort data-type="number"
select="price/offer | price/orig[not(../offer)]"/>
</xsl:apply-templates>
</products>
</xsl:template>
</xsl:stylesheet>
IV. Xác minh:
Cả ba biến đổi trên, khi áp dụng cho các tài liệu XML cung cấp:
<products>
<product>
<name>Product 1</name>
<price>
<orig>15</orig>
<offer>10</offer>
</price>
</product>
<product>
<name>Product 2</name>
<price>
<orig>13</orig>
<offer>12</offer>
</price>
</product>
<product>
<name>Product 3</name>
<price>
<orig>11</orig>
</price>
</product>
</products>
sản xuất mong muốn, đúng kết quả:
<products>
<product>
<name>Product 1</name>
<price>
<orig>15</orig>
<offer>10</offer>
</price>
</product>
<product>
<name>Product 3</name>
<price>
<orig>11</orig>
</price>
</product>
<product>
<name>Product 2</name>
<price>
<orig>13</orig>
<offer>12</offer>
</price>
</product>
</products>
Giải pháp II là duy nhất của ba vẫn tạo ra kết quả chính xác khi được áp dụng trên tài liệu XML này (đã thêm minAcceptable
con vào price
):
<products>
<product>
<name>Product 1</name>
<price>
<orig>15</orig>
<offer>10</offer>
<minAcceptable>8</minAcceptable>
</price>
</product>
<product>
<name>Product 2</name>
<price>
<orig>13</orig>
<offer>12</offer>
<minAcceptable>6</minAcceptable>
</price>
</product>
<product>
<name>Product 3</name>
<price>
<orig>11</orig>
<minAcceptable>7</minAcceptable>
</price>
</product>
</products>
Do lưu ý rằng không có câu trả lời khác xử lý tài liệu XML này một cách chính xác.
+1 Rất thanh lịch - công việc tuyệt vời, @DimitreNovatchev. Tuy nhiên, tôi cho rằng Solution II không phù hợp với câu hỏi (vì nó giải quyết một tình huống không bao giờ được mô tả bởi OP) và do đó không nên được sử dụng làm bằng chứng về sự không đầy đủ trong các câu trả lời khác. :) – ABach
@ABach, Bạn được chào đón. Đối với sự liên quan, cả giải pháp 1. và 3. đều chính xác theo tài liệu XML của OP. Giải pháp 2 cung cấp cho chúng tôi kiến thức phải làm gì trong một tình huống hơi khác, khi giải pháp khác không hoạt động. Kiến thức là sức mạnh, bạn không thonk như vậy? –
Tôi sẽ mua nó. :) – ABach