2017-07-07 51 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> 

如果您想进一步细化订单,例如按艺术家按字母顺序显示剩余记录,然后您可以添加任意数量的附加排序键;根据它们出现的顺序,适用于相同范围的多个键从最大到最小被赋予重要性。