需要一些快速的家庭作业帮助。 XSL Muenchian分组不正常

时间:2014-05-04 21:59:30

标签: xml xslt grouping

这是我在尝试将它们组合在一起时使用的内容。 基本上我需要帮助的部分是我需要使用Muenchian分组显示电影,同时我需要已经列出的电影不再显示。

这是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="b2">
   <movie>A DAY AT THE RACES (1937)</movie>
   <movie>A SHOT IN THE DARK (1964)</movie>
   <movie>AMERICAN GRAFFITI (1973)</movie>
   <movie>BALL OF FIRE (1941)</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>SHAMPOO (1975)</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>`

这是我的代码......

 `<?xml version="1.0" encoding="UTF-8" ?>
<!--
   New Perspectives on XML
   Tutorial 8
   Case Problem 1

   Best American Comedies XSLT Style Sheet
   Author: Thomas Collins
   Date:   5/2/2014   

   Filename:         comedy.xsl
   Supporting Files: comedy.css
-->

<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="movies" match="ballot" use="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: <xsl:value-of select="count(ballot)"/>

      </p>

      <table>
           <tr>
              <th>Rank</th>
              <th>Movie</th>
              <th>Votes</th>
              <th>%</th>

          </tr>
              <xsl:if test="position()=1">
                <td class="tdtext" rowspan="{count(../../ballot/movie)}">
                    <xsl:value-of select="../../@movie" />
                </td>
            </xsl:if>
            <xsl:for-each select="//movie[generate-id(key('movies', .)[1])]">


                <tr>
                    <td><xsl:value-of select="position()"/></td>
                    <td>

                    <xsl:value-of select="//movie[generate-id(key('movies',.)[1])]" /> 




                    </td>
                    <td class="right">
                        <xsl:value-of select="count(key('movies', .))"/>

                    </td>
                    <td class="right">percent</td>
                        <tr>
                            <td colspan="4"><hr /></td>
                        </tr>

                </tr>
          </xsl:for-each>
      </table>

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

    <xsl:template match="ballot" mode="cityList">
    <a href="#{generate-id()}">
    <xsl:value-of select="movie" />
    </a>
    (<xsl:value-of select="count(key('movies', movie))"/>)|
    </xsl:template>

</xsl:stylesheet>`

但这就是我得到的......

http://fa-iwp-01.macomb.edu/0900964/itwp2400/Project5/comedy.xml

1 个答案:

答案 0 :(得分:0)

以下XSLT将提供选票统计的主表。您的主要错误在于评估for-each表达式中的关键函数。我添加了对 percent 列的一些解释。 : - )

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:key name="movies" match="ballot" use="movie" />

  <xsl:template match="/">
    <xsl:variable name="nr_of_ballots" select="count(poll/ballot)"/>
    <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: <xsl:value-of select="$nr_of_ballots"/>
        </p>

        <table>
          <tr>
            <th>Rank</th>
            <th>Movie</th>
            <th>Votes</th>
            <th>%</th>
          </tr>

          <xsl:for-each select="//movie[generate-id(..) = generate-id(key('movies', .)[1])]">
            <xsl:sort select="count(key('movies', .))" data-type="number" order="descending"/>

            <tr>
              <td><xsl:value-of select="position()"/></td>
              <td>
                <xsl:value-of select="." /> 
              </td>
              <td class="right">
                <xsl:value-of select="count(key('movies', .))"/>
              </td>
              <td class="right">
                <xsl:value-of select="format-number(100 * count(key('movies', .)) div $nr_of_ballots, '#.0')"/>
              </td>
              <tr>
                <td colspan="4"><hr /></td>
              </tr>
            </tr>
          </xsl:for-each>
        </table>

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

</xsl:stylesheet>