2013-04-09 30 views
3

我正在使用xslt將xml文檔轉換爲html表格,現在我想要做的是更改包含最低「價格」值的單元格的背景顏色,而無需整理我的列表。從xml中選擇最低值而不排序

我是新來的,所以我在做的是遵循W3C學校的例子。該XML文件看起來像這樣:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!-- Edited by XMLSpy® --> 
<catalog> 
<cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
</cd> 
<cd> 
    <title>Black angel</title> 
    <artist>Savage Rose</artist> 
    <country>EU</country> 
    <company>Mega</company> 
    <price>11.90</price> 
    <year>1995</year> 
</cd> 
<cd> 
    <title>For the good times</title> 
    <artist>Kenny Rogers</artist> 
    <country>UK</country> 
    <company>Mucik Master</company> 
    <price>8.70</price> 
    <year>1995</year> 
</cd> 
</catalog> 

而我想獲得的是類似的東西,但沒有按價格排序列表中的元素。所以我想維護XML文檔中的原始順序。

<?xml version="1.0" encoding="ISO-8859-1"?> 
    <!-- Edited by XMLSpy® --> 
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
      <th>Title</th> 
      <th>Artist</th> 
      <th>Price</th> 
     </tr> 
     <xsl:for-each select="catalog/cd"> 
     <xsl:sort select="price" order="ascending" data-type="number"/> 
      <tr> 
      <td><xsl:value-of select="title"/></td> 
      <td><xsl:value-of select="artist"/></td> 
      <xsl:choose> 
       <xsl:when test="(position() = 1)"> 
       <td bgcolor="#ff00ff"> 
       <xsl:value-of select="price"/></td> 
       </xsl:when> 
      <xsl:otherwise> 
       <td><xsl:value-of select="price"/></td> 
      </xsl:otherwise> 
      </xsl:choose> 
      </tr> 
     </xsl:for-each> 
     </table> 
     </body> 
     </html> 
    </xsl:template> 
    </xsl:stylesheet> 

在此先感謝您的幫助。


編輯:我想我已經找到了我的問題的解決方案:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!-- Edited by XMLSpy® --> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Artist</th> 
     <th>Price</th> 
     </tr> 
     <xsl:for-each select="catalog/cd"> 
     <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="artist"/></td> 
     <xsl:choose> 
      <xsl:when test="price=/catalog/cd/price[not(. &gt; ../../cd/price)][1]"> 
      <td bgcolor="#ff00ff"> 
      <xsl:value-of select="price"/></td> 
      </xsl:when> 
     <xsl:otherwise> 
      <td><xsl:value-of select="price"/></td> 
     </xsl:otherwise> 
     </xsl:choose> 
     </tr> 

     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

+0

user 2260857,sor ting用於查找最小值並不意味着任何元素將被重新排序。使用''查找最小值實際上是XSLT 1.0中最快的方式 - 並且不需要任何擴展函數 – 2013-04-10 14:30:54

回答

2

你似乎已經回答了你的問題,但我會在一對夫婦的其他東西扔在這裏。

(1),則可以避免通過增加一個可變的樣式表或模板做計算一次重新計算在每次迭代的最低價格通過<cd/>元素:

<xsl:variable name="least" select="math:min(/catalog/cd/price)"/> 

(2)即「$ least」變量聲明使用EXSLT math:min函數(命名空間是xmlns:math =「http://exslt.org/math」),它可能適用於您的XSLT 1.0處理器。如果你有一個2.0處理器,你可以使用內建的「min()」。

如果沒有別的,min()函數對下一個人更具可讀性。

(3)這是純粹的風格,但您可以通過使用xsl:屬性來減少分支。

<td> 
    <xsl:if test="price=$lowest"> 
     <xsl:attribute name="bgcolor">#ff00ff</xsl:attribute> 
    </xsl:if> 
    <xsl:value-of select="price"/> 
</td> 
2

而我想獲得與此類似,但沒有 按價格排序列表中的元素的東西。

排序用於查找最小值並不意味着任何元素將被重新排序。使用<xsl:sort>查找最小值實際上是XSLT 1中最快的方法。0 - 和要求任何擴展函數

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:variable name="vLowest"> 
    <xsl:for-each select="/*/*/price"> 
     <xsl:sort data-type="number"/> 
     <xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if> 
    </xsl:for-each> 
    </xsl:variable> 

<xsl:template match="/*"> 
      <html> 
      <body> 
      <h2>My CD Collection</h2> 
      <table border="1"> 
       <tr bgcolor="#9acd32"> 
       <th>Title</th> 
       <th>Artist</th> 
       <th>Price</th> 
       </tr> 
       <xsl:apply-templates select="cd"/> 
      </table> 
      </body> 
      </html> 
</xsl:template> 

<xsl:template match="cd"> 
     <tr> 
      <td><xsl:value-of select="title"/></td> 
      <td><xsl:value-of select="artist"/></td> 
      <td> 
      <xsl:if test="price=number($vLowest)"> 
       <xsl:attribute name="bgcolor">#ff00ff</xsl:attribute> 
      </xsl:if> 
      <xsl:value-of select="price"/> 
      </td> 
     </tr> 
</xsl:template> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔應用:

<catalog> 
    <cd> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
     <year>1985</year> 
    </cd> 
    <cd> 
     <title>Black angel</title> 
     <artist>Savage Rose</artist> 
     <country>EU</country> 
     <company>Mega</company> 
     <price>11.90</price> 
     <year>1995</year> 
    </cd> 
    <cd> 
     <title>For the good times</title> 
     <artist>Kenny Rogers</artist> 
     <country>UK</country> 
     <company>Mucik Master</company> 
     <price>8.70</price> 
     <year>1995</year> 
    </cd> 
</catalog> 

有用,正確的結果產生:

<html> 
    <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
     <tr bgcolor="#9acd32"> 
      <th>Title</th> 
      <th>Artist</th> 
      <th>Price</th> 
     </tr> 
     <tr> 
      <td>Empire Burlesque</td> 
      <td>Bob Dylan</td> 
      <td>10.90</td> 
     </tr> 
     <tr> 
      <td>Black angel</td> 
      <td>Savage Rose</td> 
      <td>11.90</td> 
     </tr> 
     <tr> 
      <td>For the good times</td> 
      <td>Kenny Rogers</td> 
      <td bgcolor="#ff00ff">8.70</td> 
     </tr> 
     </table> 
    </body> 
</html>