2017-07-07 60 views
1

我有要求首先顯示特定的字符串記錄,例如在這裏我想顯示所有Rehman記錄首先,然後所有其他沒有特定的順序。如何在xsl中的for reach循環中首先顯示特定記錄

XML

<?xml version="1.0" encoding="UTF-8"?> 
<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>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
    </cd> 
    <cd> 
    <title>Roja</title> 
    <artist>Rehman</artist> 
    <country>USA</country> 
    <company>RCA</company> 
    <price>9.90</price> 
    <year>1982</year> 
    </cd> 
    <cd> 
    <title>Still got the blues</title> 
    <artist>Gary Moore</artist> 
    <country>UK</country> 
    <company>Virgin records</company> 
    <price>10.20</price> 
    <year>1990</year> 
    </cd> 
    <cd> 
    <title>Eros</title> 
    <artist>Zack</artist> 
    <country>EU</country> 
    <company>BMG</company> 
    <price>9.90</price> 
    <year>1997</year> 
    </cd> 
    <cd> 
    <title>Rockstar</title> 
    <artist>Rehman</artist> 
    <country>UK</country> 
    <company>Polydor</company> 
    <price>10.90</price> 
    <year>1998</year> 
    </cd> 
    <cd> 
    <title>Sylvias Mother</title> 
    <artist>Dr.Hook</artist> 
    <country>UK</country> 
    <company>CBS</company> 
    <price>8.10</price> 
    <year>1973</year> 
    </cd> 

</catalog> 

XSL

<?xml version="1.0" encoding="UTF-8"?> 
<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> 
     </tr> 
     <xsl:for-each select="catalog/cd"> 
     <xsl:sort select="artist" order ="descending"/> 
     <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="artist"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

結果

自己收藏的CD

標題藝術家

Empire Burlesque Bob Dylan 
Hide your heart Bonnie Tyler 
Sylvias Mother Dr.Hook 
Still got the blues Gary Moore 
Roja Rehman 
Rockstar Rehman 
Eros Zack 

回答

0

您可以使用此

<?xml version="1.0" encoding="UTF-8"?> 
<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> 
        </tr> 
        <xsl:for-each select="catalog/cd[artist='Rehman']"> 
         <xsl:sort select="artist" order ="descending"/> 
         <tr> 
          <td><xsl:value-of select="title"/></td> 
          <td><xsl:value-of select="artist"/></td> 
         </tr> 
        </xsl:for-each> 
        <xsl:for-each select="catalog/cd[artist!='Rehman']"> 
         <!-- You can use this for sorting of rest --> 
         <!-- <xsl:sort select="artist" order ="descending"/> --> 
         <tr> 
          <td><xsl:value-of select="title"/></td> 
          <td><xsl:value-of select="artist"/></td> 
         </tr> 
        </xsl:for-each> 

       </table> 
      </body> 
     </html> 
    </xsl:template> 

</xsl:stylesheet> 
+0

在這裏,我們通常喜歡一些解釋性的評論來陪伴我們的代碼,因爲畢竟,它通常是實際回答問題的評論。隨附的 - 非常受歡迎的代碼*澄清*。 –

0

你似乎已經擁有你需要的大部分部件;這只是一個以有效的方式組裝它們的問題。特別是,您可以通過精心挑選的xsl:sort鍵完成此操作。

你想與藝術家拉赫曼的光盤是所有的在別人面前輸出,所以讓我們開始時用<cd>元素作爲當前節點進行評估,表達這一條件的XPath表達式:

artist = 'Rehman' 

A排序鍵可以是任何XPath表達式;它的值將被轉換爲一個字符串(並可能隨後被重新解釋爲一個數字)。上面的表達式評估爲布爾值,並且轉換爲將導致'true''false'的字符串。由於'true''false'後按字典來了,你想爲哪個表達式爲true的光盤是輸出第一,你要指定該鍵降序排列:

 <xsl:for-each select="catalog/cd"> 
     <xsl:sort select="artist = 'Rehman'" order="descending"/> 
     <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="artist"/></td> 
     </tr> 
     </xsl:for-each> 

如果您想進一步細化訂單,例如按藝術家按字母順序顯示剩餘記錄,然後您可以添加任意數量的附加排序鍵;根據它們出現的順序,適用於相同範圍的多個鍵從最大到最小被賦予重要性。