Как рассчитать количество показов фильма, за которое проголосовали. - PullRequest
1 голос
/ 30 июля 2011

Вот мой лист XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="movie" match="movie" use="." /> 
<xsl:template match="/">
<html>
<head>
<title>Top American Comedies</title> 
<link href="comedy.css" rel="stylesheet" type="text/css" /> 
</head>
<body>
<h2>The Top American Comedy Films</h2> 
<p>Number of Ballots:162</p> 
<table>
<tr>
<th>Rank</th> 
<th>Movie</th> 
<th>Votes</th> 
<th>%</th> 
<xsl:for-each select="*/*/movie">
      <tr>
      <td><xsl:value-of select="position()"/></td>
      <td><xsl:value-of select="."/></td>
      <td class="right" />
      <td class="right" />
      </tr>
      </xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Вот мой код XML

<?xml-stylesheet type="text/xsl" href="comtxt.xsl" ?>
<poll>
<ballot id="b1">
<movie>A FISH CALLED WANDA (1988)</movie>
<movie>ADAM'S RIB (1949)</movie>
<movie>ANNIE HALL (1977)</movie>
<movie>BEING THERE (1979)</movie>
<movie>BORN YESTERDAY (1950)</movie>
 <movie>CITY LIGHTS (1931)</movie>
<movie>DR. STRANGELOVE (1964)</movie>
<movie>GOOD MORNING, VIETNAM (1987)</movie>
<movie>HIS GIRL FRIDAY (1940)</movie>
<movie>M*A*S*H (1970)</movie>
<movie>MOONSTRUCK (1987)</movie>
<movie>NATIONAL LAMPOON'S ANIMAL HOUSE (1978)</movie>
<movie>SILVER STREAK (1976)</movie>
<movie>SOME LIKE IT HOT (1959)</movie>
<movie>THE GENERAL (1927)</movie>
<movie>THE GRADUATE (1967)</movie>
<movie>THE ODD COUPLE (1968)</movie>
<movie>THE SEVEN YEAR ITCH (1955)</movie>
<movie>TO BE OR NOT TO BE (1942)</movie>
<movie>WHAT'S UP, DOC? (1972)</movie>
</ballot>    
<ballot id="b162">
<movie>A DAY AT THE RACES (1937)</movie>
<movie>ABBOTT AND COSTELLO MEET FRANKENSTEIN (1948)</movie>
<movie>AMERICAN GRAFFITI (1973)</movie>
<movie>BANANAS (1971)</movie>
<movie>BLAZING SADDLES (1974)</movie>
<movie>CADDYSHACK (1980)</movie>
<movie>DR. STRANGELOVE (1964)</movie>
<movie>GHOSTBUSTERS (1984)</movie>
<movie>HIS GIRL FRIDAY (1940)</movie>
<movie>IT'S A MAD MAD MAD MAD WORLD (1963)</movie>
<movie>MODERN TIMES (1936)</movie>
<movie>NATIONAL LAMPOON'S ANIMAL HOUSE (1978)</movie>
<movie>SHE DONE HIM WRONG (1933)</movie>
<movie>SOME LIKE IT HOT (1959)</movie>
<movie>THE AWFUL TRUTH (1937)</movie>
<movie>THE GRADUATE (1967)</movie>
<movie>THE ODD COUPLE (1968)</movie>
<movie>THE PRODUCERS (1968)</movie>
<movie>THIS IS SPINAL TAP (1984)</movie>
<movie>TOPPER (1937)</movie>
</ballot>
</poll>

Список фильмов гораздо больше.У меня вопрос, как мне рассчитать, сколько раз фильм был показан в этом списке.Так что, если фильм был показан 10 раз, я хочу, чтобы он показывался под голосованием.

1 Ответ

0 голосов
/ 30 июля 2011

теперь вижу ...

Это преобразование использует мюнхенскую группировку для получения желаемого результата :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:key name="kMovieByName" match="movie"
  use="."/>

 <xsl:variable name="vTotalNumber"
      select="count(/*/*/movie)"/>

 <xsl:template match="/">
  <html>
    <head>
     <title>Top American Comedies</title>
     <link href="comedy.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <h2>The Top American Comedy Films</h2>
        <p>Number of Ballots:162</p>
        <table>
            <tr>
                <th>Rank</th>
                <th>Movie</th>
                <th>Votes</th>
                <th>%</th>
                <xsl:apply-templates select=
                 "/*/*/movie
                        [generate-id()
                        =
                         generate-id(key('kMovieByName',.)
                                   [1])
                        ]
                 ">
                 <xsl:sort select="count(key('kMovieByName',.))"
                           data-type="number"
                           order="descending"/>
                </xsl:apply-templates>
            </tr>
        </table>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="movie">
   <xsl:variable name="vCountThisMovie" select=
    "count(key('kMovieByName',.))"/>

    <tr>
        <td><xsl:value-of select="position()"/></td>
        <td>
          <xsl:value-of select="."/>
        </td>
        <td class="right">
          <xsl:value-of select="$vCountThisMovie"/>
        </td>
        <td class="right">
         <xsl:value-of select=
             "100 * $vCountThisMovie div $vTotalNumber"/>
        </td>
    </tr>
 </xsl:template>
</xsl:stylesheet>

При применении к предоставленному документу XML :

<poll>
    <ballot id="b1">
        <movie>A FISH CALLED WANDA (1988)</movie>
        <movie>ADAM'S RIB (1949)</movie>
        <movie>ANNIE HALL (1977)</movie>
        <movie>BEING THERE (1979)</movie>
        <movie>BORN YESTERDAY (1950)</movie>
        <movie>CITY LIGHTS (1931)</movie>
        <movie>DR. STRANGELOVE (1964)</movie>
        <movie>GOOD MORNING, VIETNAM (1987)</movie>
        <movie>HIS GIRL FRIDAY (1940)</movie>
        <movie>M*A*S*H (1970)</movie>
        <movie>MOONSTRUCK (1987)</movie>
        <movie>NATIONAL LAMPOON'S ANIMAL HOUSE (1978)</movie>
        <movie>SILVER STREAK (1976)</movie>
        <movie>SOME LIKE IT HOT (1959)</movie>
        <movie>THE GENERAL (1927)</movie>
        <movie>THE GRADUATE (1967)</movie>
        <movie>THE ODD COUPLE (1968)</movie>
        <movie>THE SEVEN YEAR ITCH (1955)</movie>
        <movie>TO BE OR NOT TO BE (1942)</movie>
        <movie>WHAT'S UP, DOC? (1972)</movie>
    </ballot>
    <ballot id="b162">
        <movie>A DAY AT THE RACES (1937)</movie>
        <movie>ABBOTT AND COSTELLO MEET FRANKENSTEIN (1948)</movie>
        <movie>AMERICAN GRAFFITI (1973)</movie>
        <movie>BANANAS (1971)</movie>
        <movie>BLAZING SADDLES (1974)</movie>
        <movie>CADDYSHACK (1980)</movie>
        <movie>DR. STRANGELOVE (1964)</movie>
        <movie>GHOSTBUSTERS (1984)</movie>
        <movie>HIS GIRL FRIDAY (1940)</movie>
        <movie>IT'S A MAD MAD MAD MAD WORLD (1963)</movie>
        <movie>MODERN TIMES (1936)</movie>
        <movie>NATIONAL LAMPOON'S ANIMAL HOUSE (1978)</movie>
        <movie>SHE DONE HIM WRONG (1933)</movie>
        <movie>SOME LIKE IT HOT (1959)</movie>
        <movie>THE AWFUL TRUTH (1937)</movie>
        <movie>THE GRADUATE (1967)</movie>
        <movie>THE ODD COUPLE (1968)</movie>
        <movie>THE PRODUCERS (1968)</movie>
        <movie>THIS IS SPINAL TAP (1984)</movie>
        <movie>TOPPER (1937)</movie>
    </ballot>
</poll>

желаемый, правильный результат получается :

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <title>Top American Comedies</title>
      <link href="comedy.css" rel="stylesheet" type="text/css">
   </head>
   <body>
      <h2>The Top American Comedy Films</h2>
      <p>Number of Ballots:162</p>
      <table>
         <tr>
            <th>Rank</th>
            <th>Movie</th>
            <th>Votes</th>
            <th>%</th>
            <tr>
               <td>1</td>
               <td>DR. STRANGELOVE (1964)</td>
               <td class="right">2</td>
               <td class="right">5</td>
            </tr>
            <tr>
               <td>2</td>
               <td>HIS GIRL FRIDAY (1940)</td>
               <td class="right">2</td>
               <td class="right">5</td>
            </tr>
            <tr>
               <td>3</td>
               <td>NATIONAL LAMPOON'S ANIMAL HOUSE (1978)</td>
               <td class="right">2</td>
               <td class="right">5</td>
            </tr>
            <tr>
               <td>4</td>
               <td>SOME LIKE IT HOT (1959)</td>
               <td class="right">2</td>
               <td class="right">5</td>
            </tr>
            <tr>
               <td>5</td>
               <td>THE GRADUATE (1967)</td>
               <td class="right">2</td>
               <td class="right">5</td>
            </tr>
            <tr>
               <td>6</td>
               <td>THE ODD COUPLE (1968)</td>
               <td class="right">2</td>
               <td class="right">5</td>
            </tr>
            <tr>
               <td>7</td>
               <td>A FISH CALLED WANDA (1988)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>8</td>
               <td>ADAM'S RIB (1949)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>9</td>
               <td>ANNIE HALL (1977)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>10</td>
               <td>BEING THERE (1979)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>11</td>
               <td>BORN YESTERDAY (1950)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>12</td>
               <td>CITY LIGHTS (1931)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>13</td>
               <td>GOOD MORNING, VIETNAM (1987)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>14</td>
               <td>M*A*S*H (1970)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>15</td>
               <td>MOONSTRUCK (1987)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>16</td>
               <td>SILVER STREAK (1976)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>17</td>
               <td>THE GENERAL (1927)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>18</td>
               <td>THE SEVEN YEAR ITCH (1955)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>19</td>
               <td>TO BE OR NOT TO BE (1942)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>20</td>
               <td>WHAT'S UP, DOC? (1972)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>21</td>
               <td>A DAY AT THE RACES (1937)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>22</td>
               <td>ABBOTT AND COSTELLO MEET FRANKENSTEIN (1948)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>23</td>
               <td>AMERICAN GRAFFITI (1973)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>24</td>
               <td>BANANAS (1971)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>25</td>
               <td>BLAZING SADDLES (1974)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>26</td>
               <td>CADDYSHACK (1980)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>27</td>
               <td>GHOSTBUSTERS (1984)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>28</td>
               <td>IT'S A MAD MAD MAD MAD WORLD (1963)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>29</td>
               <td>MODERN TIMES (1936)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>30</td>
               <td>SHE DONE HIM WRONG (1933)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>31</td>
               <td>THE AWFUL TRUTH (1937)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>32</td>
               <td>THE PRODUCERS (1968)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>33</td>
               <td>THIS IS SPINAL TAP (1984)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
            <tr>
               <td>34</td>
               <td>TOPPER (1937)</td>
               <td class="right">1</td>
               <td class="right">2.5</td>
            </tr>
         </tr>
      </table>
   </body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...