下面是我的xml
<products>
<product>
<supplier>1</supplier>
<item>
<color>Red</color>
<name>Pen</name>
<price>10</price>
</item>
<item>
<color>Green</color>
<name>Pen</name>
<price>15</price>
</item>
<item>
<color>Red</color>
<name>Pen</name>
<price>5</price>
</item>
</product>
<product>
<supplier>1</supplier>
<item>
<color>White</color>
<name>Pen</name>
<price>10</price>
</item>
<item>
<color>Green</color>
<name>Pen</name>
<price>18</price>
</item>
<item>
<color>Red</color>
<name>Pen</name>
<price>15</price>
</item>
</product>
<product>
<supplier>2</supplier>
<item>
<color>White</color>
<name>Pen</name>
<price>5</price>
</item>
<item>
<color>Green</color>
<name>Pen</name>
<price>18</price>
</item>
<item>
<color>White</color>
<name>Pen</name>
<price>15</price>
</item>
</product>
</products>
我需要以最低价格采用不同的产品...使用xslt 1.0
所需的输出是
<products>
<product>
<supplier>1</supplier>
<item>
<color>Green</color>
<name>Pen</name>
<price>15</price>
</item>
<item>
<color>Red</color>
<name>Pen</name>
<price>5</price>
</item>
<item>
<color>White</color>
<name>Pen</name>
<price>10</price>
</item>
</product>
<product>
<supplier>2</supplier>
<item>
<color>White</color>
<name>Pen</name>
<price>5</price>
</item>
<item>
<color>Green</color>
<name>Pen</name>
<price>18</price>
</item>
</product>
</products>
希望得到你的帮助-------------------------------------------- -------------------- ------------------------------ --------- - ---------------------------------------- --------------------------- - - --------------------- ---------
答案 0 :(得分:0)
使用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="s" match="product" use="supplier"/>
<xsl:key name="k" match="item" use="concat(../supplier, ',', name, ',', color)"/>
<xsl:template match="/products">
<xsl:copy>
<xsl:apply-templates select="product[generate-id()
= generate-id(key('s', supplier))]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="product">
<xsl:copy>
<xsl:copy-of select="supplier"/>
<xsl:apply-templates select="../product/item[generate-id()
= generate-id(key('k', concat(current()/supplier, ',', name, ',', color)))]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<xsl:for-each select="key('k', concat(../supplier, ',', name, ',', color))">
<xsl:sort select="price" data-type="number"/>
<xsl:if test="position() = 1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>