需要使用xslt获取最大行数

时间:2016-12-07 20:05:29

标签: xml xslt xslt-2.0

需要帮助xml / xsl需要为最大计数国家/地区加下划线 我对它们进行排序,并按每个国家/地区输出,但我需要在最大行数下划线的国家(在我的情况下是英国) 我输入了xml

<?xml version="1.0"?>


<CATALOG>
  <CD path="www.mail.ru">
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD path="www.mail.ru">
    <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>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
  </CD>
  <CD path="www.mail.ru">
    <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>Eros Ramazzotti</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>BMG</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1997</YEAR>
  </CD>
  <CD>
    <TITLE>One night only</TITLE>
    <ARTIST>Bee Gees</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1998</YEAR>
  </CD>
  <CD path="www.mail.ru">
    <TITLE>Sylvias Mother</TITLE>
    <ARTIST>Dr.Hook</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS</COMPANY>
    <PRICE>8.10</PRICE>
    <YEAR>1973</YEAR>
  </CD>
  <CD>
    <TITLE>Maggie May</TITLE>
    <ARTIST>Rod Stewart</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Pickwick</COMPANY>
    <PRICE>8.50</PRICE>
    <YEAR>1990</YEAR>
  </CD>
  <CD>
    <TITLE>Romanza</TITLE>
    <ARTIST>Andrea Bocelli</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.80</PRICE>
    <YEAR>1996</YEAR>
  </CD>
  <CD>
    <TITLE>When a man loves a woman</TITLE>
    <ARTIST>Percy Sledge</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Atlantic</COMPANY>
    <PRICE>8.70</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Black angel</TITLE>
    <ARTIST>Savage Rose</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Mega</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1995</YEAR>
  </CD>
  <CD>
    <TITLE>1999 Grammy Nominees</TITLE>
    <ARTIST>Many</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Grammy</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1999</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>
  <CD>
    <TITLE>Big Willie style</TITLE>
    <ARTIST>Will Smith</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1997</YEAR>
  </CD>
  <CD>
    <TITLE>Tupelo Honey</TITLE>
    <ARTIST>Van Morrison</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>8.20</PRICE>
    <YEAR>1971</YEAR>
  </CD>
  <CD>
    <TITLE>Soulsville</TITLE>
    <ARTIST>Jorn Hoel</ARTIST>
    <COUNTRY>Norway</COUNTRY>
    <COMPANY>WEA</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1996</YEAR>
  </CD>
  <CD>
    <TITLE>The very best of</TITLE>
    <ARTIST>Cat Stevens</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Island</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1990</YEAR>
  </CD>
  <CD>
    <TITLE>Stop</TITLE>
    <ARTIST>Sam Brown</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>A and M</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <CD>
    <TITLE>Bridge of Spies</TITLE>
    <ARTIST>T`Pau</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Siren</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Private Dancer</TITLE>
    <ARTIST>Tina Turner</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Capitol</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1983</YEAR>
  </CD>
  <CD>
    <TITLE>Midt om natten</TITLE>
    <ARTIST>Kim Larsen</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Medley</COMPANY>
    <PRICE>7.80</PRICE>
    <YEAR>1983</YEAR>
  </CD>
  <CD>
    <TITLE>Pavarotti Gala Concert</TITLE>
    <ARTIST>Luciano Pavarotti</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>DECCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1991</YEAR>
  </CD>
  <CD>
    <TITLE>The dock of the bay</TITLE>
    <ARTIST>Otis Redding</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Atlantic</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Picture book</TITLE>
    <ARTIST>Simply Red</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Elektra</COMPANY>
    <PRICE>7.20</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Red</TITLE>
    <ARTIST>The Communards</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>London</COMPANY>
    <PRICE>7.80</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Unchain my heart</TITLE>
    <ARTIST>Joe Cocker</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>EMI</COMPANY>
    <PRICE>8.20</PRICE>
    <YEAR>1987</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>CDs</h2>
 <table border="1">
    <xsl:for-each-group select="CATALOG/CD" group-by="COUNTRY">
        <td bgcolor="#32a6cd"><xsl:value-of select="COUNTRY"/></td>
        <td bgcolor="#32a6cd">PRICE</td>
    <tr>
        <xsl:for-each select="current-group()">
        <tr>
        <td><xsl:value-of select="TITLE"/></td>
        <xsl:choose>

        <xsl:when test="PRICE>9">
            <td bgcolor="#d10c20">
                <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>
    <tr></tr>
    </tr>
</xsl:for-each-group>

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

out html

<html>
   <body>
      <h2>CDs</h2>
      <table border="1">
         <td bgcolor="#32a6cd">USA</td>
         <td bgcolor="#32a6cd">PRICE</td>
         <tr>
            <tr>
               <td>Empire Burlesque</td>
               <td bgcolor="#d10c20">10.90</td>
            </tr>
            <tr>
               <td>Greatest Hits</td>
               <td bgcolor="#d10c20">9.90</td>
            </tr>
            <tr>
               <td>When a man loves a woman</td>
               <td>8.70</td>
            </tr>
            <tr>
               <td>1999 Grammy Nominees</td>
               <td bgcolor="#d10c20">10.20</td>
            </tr>
            <tr>
               <td>Big Willie style</td>
               <td bgcolor="#d10c20">9.90</td>
            </tr>
            <tr>
               <td>The dock of the bay</td>
               <td>7.90</td>
            </tr>
            <tr>
               <td>Unchain my heart</td>
               <td>8.20</td>
            </tr>
            <tr></tr>
         </tr>
         <td bgcolor="#32a6cd">UK</td>
         <td bgcolor="#32a6cd">PRICE</td>
         <tr>
            <tr>
               <td>Hide your heart</td>
               <td bgcolor="#d10c20">9.90</td>
            </tr>
            <tr>
               <td>Still got the blues</td>
               <td bgcolor="#d10c20">10.20</td>
            </tr>
            <tr>
               <td>One night only</td>
               <td bgcolor="#d10c20">10.90</td>
            </tr>
            <tr>
               <td>Sylvias Mother</td>
               <td>8.10</td>
            </tr>
            <tr>
               <td>Maggie May</td>
               <td>8.50</td>
            </tr>
            <tr>
               <td>For the good times</td>
               <td>8.70</td>
            </tr>
            <tr>
               <td>Tupelo Honey</td>
               <td>8.20</td>
            </tr>
            <tr>
               <td>The very best of</td>
               <td>8.90</td>
            </tr>
            <tr>
               <td>Stop</td>
               <td>8.90</td>
            </tr>
            <tr>
               <td>Bridge of Spies</td>
               <td>7.90</td>
            </tr>
            <tr>
               <td>Private Dancer</td>
               <td>8.90</td>
            </tr>
            <tr>
               <td>Pavarotti Gala Concert</td>
               <td bgcolor="#d10c20">9.90</td>
            </tr>
            <tr>
               <td>Red</td>
               <td>7.80</td>
            </tr>
            <tr></tr>
         </tr>
         <td bgcolor="#32a6cd">EU</td>
         <td bgcolor="#32a6cd">PRICE</td>
         <tr>
            <tr>
               <td>Eros</td>
               <td bgcolor="#d10c20">9.90</td>
            </tr>
            <tr>
               <td>Romanza</td>
               <td bgcolor="#d10c20">10.80</td>
            </tr>
            <tr>
               <td>Black angel</td>
               <td bgcolor="#d10c20">10.90</td>
            </tr>
            <tr>
               <td>Midt om natten</td>
               <td>7.80</td>
            </tr>
            <tr>
               <td>Picture book</td>
               <td>7.20</td>
            </tr>
            <tr></tr>
         </tr>
         <td bgcolor="#32a6cd">Norway</td>
         <td bgcolor="#32a6cd">PRICE</td>
         <tr>
            <tr>
               <td>Soulsville</td>
               <td>7.90</td>
            </tr>
            <tr></tr>
         </tr>
      </table>
   </body>
</html>

英国必须加下划线或只是红色

2 个答案:

答案 0 :(得分:2)

您可以按组成员的数量对组进行排序,然后设置最后一个(按升序排列)或第一个按降序排序,以便更改

<xsl:for-each-group select="CATALOG/CD" group-by="COUNTRY">
    <td bgcolor="#32a6cd"><xsl:value-of select="COUNTRY"/></td>

<xsl:for-each-group select="CATALOG/CD" group-by="COUNTRY">
  <xsl:sort select="count(current-group())" order="descending"/>
  <td bgcolor="#32a6cd" style="{if (position() eq 1) then 'text-decoration: underline;' else ()}">
    <xsl:value-of select="COUNTRY"/>
  </td>

答案 1 :(得分: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>CDs</h2>
 <table border="1">
    <xsl:variable name="groups">
        <xsl:for-each-group select="CATALOG/CD" group-by="COUNTRY">
            <group rows="{count(current-group())}">
                <xsl:sequence select="current-group()"/>
            </group>
        </xsl:for-each-group>
    </xsl:variable>
    <xsl:for-each select="$groups/group">
        <tr>
            <td bgcolor="#32a6cd">
                <xsl:if test="count(*) = max($groups/group/@rows)">
                    <xsl:attribute name="style" select="'text-decoration: underline;'"/>
                    <xsl:attribute name="bgcolor" select="green"/>
                </xsl:if>
                <xsl:value-of select="CD[1]/COUNTRY"/>
            </td>
            <td bgcolor="#32a6cd">PRICE</td>
        </tr>
        <xsl:for-each select="*">
            <tr>
                <td><xsl:value-of select="TITLE"/></td>
                <xsl:choose>
                    <xsl:when test="PRICE>9">
                        <td bgcolor="#d10c20">
                            <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>
    </xsl:for-each>

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