2012-09-14 38 views
2

我有以下XML:XSLT排序多个子节点上,每个家长

<?xml version="1.0" encoding="ISO-8859-1"?> 
<entries> 
    <entry> 
     <title>Entry 1</title> 
       <prices> 
       <price>10</price> 
       <price>50</price> 
       </prices> 
    </entry> 
    <entry> 
     <title>Entry 2</title> 
       <prices> 
       <price>200</price> 
       <price>300</price> 
       </prices> 
    </entry> 
    <entry> 
     <title>Entry 3</title> 
       <prices> 
       <price>70</price> 
       <price>500</price> 
       </prices> 
    </entry> 
</entries> 

每一个元素都有一个以上的价码,我要选择MAX的价格,每个元素,并使用它与其他元素进行比较。

我的XSLT是:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
    <html> 
    <body> 
    <table border="1"> 
     <tr> 
     <th>Title</th> 
     <th>Highest Price</th> 
     </tr> 
     <xsl:for-each select="entries/entry"> 
     <xsl:sort select="prices/price" order="descending" data-type="number"/> 
     <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="prices/price"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

它不工作。它产生:

Title Highest Price 
Entry 2 200 
Entry 3 70 
Entry 1 10 

虽然它应该产生:

Title Highest Price 
Entry 3 500 
Entry 2 300 
Entry 1 50 

请告诉我。我不得不使用XSLT1,所以我真的不能做:

<xsl:sort select="max(prices/price)" order="descending" data-type="number"/> 

...

+1

如何在输出中使用“30”,但它甚至不在源XML中。请编辑您的源XML,以便它符合您的预期输出。最好的问候,彼得 – Peter

+0

@彼得,对不起,你是对的,我编辑它。谢谢。 –

回答

2

我不知道这是否是做的一个特别优雅的方式,但你可以遍历价格元素,而不是进入元素

<xsl:for-each select="entries/entry/prices/price"> 
    <xsl:sort select="." order="descending" data-type="number"/> 

然后,你需要一个XSL:如果条件检查每个价格元素没有一个较高的该元素

<xsl:if test="not(../price > current())"> 

以下是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
     <body> 
      <table border="1"> 
       <tr> 
        <th>Title</th> 
        <th>Highest Price</th> 
       </tr> 
       <xsl:for-each select="entries/entry/prices/price"> 
        <xsl:sort select="." order="descending" data-type="number"/> 
        <xsl:if test="not(../price > current())"> 
        <tr> 
         <td> 
          <xsl:value-of select="../../title"/> 
         </td> 
         <td> 
          <xsl:value-of select="."/> 
         </td> 
        </tr> 
        </xsl:if> 
       </xsl:for-each> 
      </table> 
     </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

当适用于您的XML,下面是输出

<html> 
<body> 
<table border="1"> 
<tr> 
<th>Title</th> 
<th>Highest Price</th> 
</tr> 
<tr> 
<td>Entry 3</td> 
<td>500</td> 
</tr> 
<tr> 
<td>Entry 2</td> 
<td>300</td> 
</tr> 
<tr> 
<td>Entry 1</td> 
<td>50</td> 
</tr> 
</table> 
</body> 
</html> 
0

看到我的回答this question

那里使用“Minimum”,但否则解决方案基本上是需要的。