2016-10-22 77 views
0

我創建了一個xml文件,其中包含具有多個屬性的15個最喜歡的電影。 xml文件可以看到下面:基於上面的xml文件我已經能夠使顯示在一個格式化表中的所有數據的模板使用xslt從xml創建兩個表,並根據它創建一個小於整數值的節點屬性

<?xml version="1.0" encoding="utf-8" ?> 
<?xml-stylesheet href="movies.xslt" type="text/xsl" ?> 

<AllMovies> 
    <movie id="01"> 
    <title>Wizard of Oz</title> 
    <director>King Vidor</director> 
    <year>1939</year> 
    <genre> 
     <g1>Classics, </g1> 
     <g2>Sci-Fi, </g2> 
     <g3>Fantasy</g3> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/the_wizard_of_oz_1939/</link> 
    </movie> 
    <movie id="02"> 
    <title>The Third Man</title> 
    <director>Carol Reed</director> 
    <year>1949</year> 
    <genre> 
     <g1>Classics, </g1> 
     <g2>Mystery, </g2> 
     <g3>Suspense</g3> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/the-third-man/</link> 
    </movie> 
    <movie id="03"> 
    <title>Citizen Kane</title> 
    <director>Orson Welles</director> 
    <year>1941</year> 
    <genre> 
     <g1>Classics, </g1> 
     <g2>Drama, </g2> 
     <g3>Mystery, </g3> 
     <g4>Suspense</g4> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/citizen_kane/</link> 
    </movie> 
    <movie id="04"> 
    <title>The Godfather</title> 
    <director>Francis Ford Coppola</director> 
    <year>1972</year> 
    <genre> 
     <g1>Suspense, </g1> 
     <g2>Drama</g2> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/godfather/</link> 
    </movie> 
    <movie id="05"> 
    <title>Inside Out</title> 
    <director>Pete Docter</director> 
    <year>2015</year> 
    <genre> 
     <g1>Comedy, </g1> 
     <g2>Animation, </g2> 
     <g3>Children</g3> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/inside_out_2015/</link> 
    </movie> 
    <movie id="06"> 
    <title>E.T. The Extra-Terrestrial</title> 
    <director>Steven Spielberg</director> 
    <year>1982</year> 
    <genre> 
     <g1>Sci-Fi, </g1> 
     <g2>Children, </g2> 
     <g3>Fantasy</g3> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/et_the_extraterrestrial/</link> 
    </movie> 
    <movie id="07"> 
    <title>Toy Story 3</title> 
    <director>Lee Unkrich</director> 
    <year>2010</year> 
    <genre> 
     <g1>Animation, </g1> 
     <g2>Comedy, </g2> 
     <g3>Children, </g3> 
     <g4>Sci-Fi</g4> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/toy_story_3/</link> 
    </movie> 
    <movie id="08"> 
    <title>Selma</title> 
    <director>Ava DuVernay</director> 
    <year>2015</year> 
    <genre> 
     <g1>Biopic, </g1> 
     <g2>History</g2> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/selma/</link> 
    </movie> 
    <movie id="09"> 
    <title>Zootopia</title> 
    <director>Bryon Howard</director> 
    <year>2016</year> 
    <genre> 
     <g1>Animation, </g1> 
     <g2>Comedy, </g2> 
     <g3>Action, </g3> 
     <g4>Adventure</g4> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/zootopia/</link> 
    </movie> 
    <movie id="10"> 
    <title>Taxi Driver</title> 
    <director>Martin Scorsese</director> 
    <year>1976</year> 
    <genre> 
     <g1>Drama, </g1> 
     <g2>Mystery, </g2> 
     <g3>Suspense</g3> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/taxi_driver/</link> 
    </movie> 
    <movie id="11"> 
    <title>The Dark Knight</title> 
    <director>Christopher Nolan</director> 
    <year>2008</year> 
    <genre> 
     <g1>Action, </g1> 
     <g2>Crime, </g2> 
     <g3>Drama</g3> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/the_dark_knight</link> 
    </movie> 
    <movie id="12"> 
    <title>The Girl with The Dragon Tattoo</title> 
    <director>David Fincher</director> 
    <year>2011</year> 
    <genre> 
     <g1>Drama, </g1> 
     <g2>Mystery, </g2> 
     <g3>Suspense</g3> 
    </genre> 
    <link>https://www.rottentomatoes.com/m/the_girl_with_the_dragon_tattoo/</link> 
    </movie> 
    <movie id="13"> 
    <title>The Last King of Scotland</title> 
    <director>Kevin Macdonald</director> 
    <year>2006</year> 
    <genre> 
     <g1>Drama, </g1> 
     <g2>Thriller</g2> 
    </genre> 
    </movie> 
    <movie id="14"> 
    <title>The Hurt Locker</title> 
    <director>Kathryn Gigelow</director> 
    <year>2009</year> 
    <genre> 
     <g1>Action, </g1> 
     <g2>Adventure, </g2> 
     <g3>Drama</g3> 
    </genre> 
    </movie> 
    <movie id="15"> 
    <title>Aliens</title> 
    <director>James Cameron</director> 
    <year>1986</year> 
    <genre> 
     <g1>Horror, </g1> 
     <g2>Thriller, </g2> 
     <g3>Adventure, </g3> 
     <g4>Sci-Fi</g4> 
    </genre> 
    </movie> 
</AllMovies> 

。我想要做的是根據每個電影節點中的<year>屬性創建兩個表格。因此,例如,我希望在2005年之前發佈的每部電影都放在第一張桌子上,然後有一個空間並添加另一張包含2005年和更新版本的所有電影的桌子。我曾嘗試在根模板和「電影」模板中使用if語句,但無法使其工作。

下面是我的XSLT代碼試圖解決的問題:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 

    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     <title>Assignment 1</title> 
     <style> 
      body{ 
      font-family: Arial, Helvetica, sans-serif 
      } 

      table{ 
      background-color: #d4ecfb; 
      border-collapse: collapse; 
      margin-left: 10px; 
      } 

      th, tr, td{ 
      border: 1px solid black; 
      } 

      #id{ 
      width:3%; 
      text-align: left; 
      padding-left: 3px; 
      } 

      #title{ 
      width:25%; 
      text-align: left; 
      } 

      #director{ 
      width:20%; 
      text-align: left; 
      } 

      #year{ 
      width: 5%; 
      text-align: left; 
      } 

      #headers{ 
      background-color: #FF69B4; 
      } 

      td{ 
      padding-left: 3px; 
      } 
     </style> 
     </head> 
     <body> 
     <h1>Top 15 Favorite Movies</h1> 
     <!-- &lt; is the hex for < and &gt; is for the > symbol--> 
     <table> 
      <tr id="headers"> 
      <th id="id">ID</th> 
      <th id="title">Title</th> 
      <th id="director">Director</th> 
      <th id="year">Year</th> 
      <th id="genre">Genre</th> 
      <th id="link">Link</th> 
      </tr> 
     <xsl:if test="year &lt; 2005"> 
      <xsl:apply-templates select="AllMovies/movie"> 
       <xsl:sort select="title" order="ascending"/> 
      </xsl:apply-templates> 
     </xsl:if> 
     <tr></tr> 
     <xsl:if test="year &gt; 2005"> 
      <xsl:apply-templates select="AllMovies/movie"> 
      <xsl:sort select="title" order="ascending"/> 
      </xsl:apply-templates> 
     </xsl:if> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="AllMovies/movie"> 
    <tr> 
     <td> 
     <xsl:value-of select="@id"/> 
     </td> 
     <td> 
     <xsl:value-of select="title"/> 
     </td> 
     <td> 
     <xsl:value-of select="director"/> 
     </td> 
     <td> 
     <xsl:value-of select="year"/> 
     </td> 
     <td> 
     <xsl:value-of select="genre"/> 
     </td> 
     <td> 
     <xsl:value-of select="link"/> 
     </td> 
    </tr> 
    </xsl:template> 
</xsl:stylesheet> 

以下是我與if語句輸出的圖像: Not working if statement

而且這個形象是沒有如果輸出聲明所有信息顯示在一個表中: Single table with all data displayed

在此先感謝您的幫助。

回答

1

你需要移動條件對year,這是檢查一個year元件相對於頂層文檔節點,以實際xsl:apply-templates SELECT子句的一部分

即取而代之的是....

<xsl:if test="year &lt; 2005"> 
    <xsl:apply-templates select="AllMovies/movie"> 
     <xsl:sort select="title" order="ascending"/> 
    </xsl:apply-templates> 
</xsl:if> 

做到這一點....

<xsl:apply-templates select="AllMovies/movie[year &lt; 2005]"> 
    <xsl:sort select="title" order="ascending"/> 
</xsl:apply-templates> 

注意,對於其它模板,您需要做&gt;=並不僅僅是&gt;

+0

請告訴我真正感興趣的是我得到了如此接近的解決方案的使用我嘗試過的各種方法,但從來沒有得到它。謝謝你,你是一個很好的幫助。 –

1

如果你想有兩個單獨的表,那麼你的主模板應沿着這些線路被構建:

<xsl:template match="/AllMovies"> 
    <html> 
     <head> 
      <title>Assignment 1</title> 
      <style> 
       /*removed for brevity*/ 
      </style> 
     </head> 
     <body> 
      <xsl:variable name="header"> 
       <tr id="headers"> 
        <th id="id">ID</th> 
        <th id="title">Title</th> 
        <th id="director">Director</th> 
        <th id="year">Year</th> 
        <th id="genre">Genre</th> 
        <th id="link">Link</th> 
       </tr> 
      </xsl:variable> 
      <h1>Before 2005</h1> 
      <table> 
       <xsl:copy-of select="$header"/> 
       <xsl:apply-templates select="movie[year &lt; 2005] "> 
        <xsl:sort select="title" order="ascending"/> 
       </xsl:apply-templates> 
      </table> 
      <h1>2005 and later</h1> 
      <table> 
       <xsl:copy-of select="$header"/> 
       <xsl:apply-templates select="movie[year >= 2005] "> 
        <xsl:sort select="title" order="ascending"/> 
       </xsl:apply-templates> 
      </table> 
     </body> 
    </html> 
</xsl:template> 
+0

謝謝邁克爾,你的解決方案很有效,但我不得不將「電影[年份< 2005]」改爲「AllMovies/movie [year .....」的完全限定位置。 –

+0

您可能錯過了我有''。 –

+0

現在我明白了,我確實。大聲笑 –

相關問題