如果在xlst上不起作用

时间:2015-10-23 19:05:03

标签: xml xslt if-statement logic

我最后一小时一直在靠墙砸墙试图弄清楚if为什么不起作用,我需要一双新的眼睛来告诉我逻辑中缺少的东西。 if在第25行之后开始。它很合适,老实说它遵循以下示例:http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=tryxsl_if但它什么也没做!

请看下面的话:



<?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>
	<!-- Background image -->
      <body background="bgimage.jpg">
		
        <h2 style="color:#47B2B2">My Movie Collection</h2>
        <!-- set border, color, and padding-->
        <table border="1" bgcolor="#0A1A1A" cellpadding="5">
          <tr bgcolor="#1F4C4C">
		  <!-- Set order -->
            <th>Title</th>
            <th>Director</th>
			<th>Year</th>
			<th>Genre</th>
			<th>ID</th>
          </tr>
          <xsl:for-each select="movies/movie">
		  <!-- Sort by title -->
            <xsl:sort select="title"/>
			<xsl:if test="year>2005">
            <tr bgcolor="#3D9999">
			
			  <td>
			  	<!-- Look for link, target to blank, the link text is the tittle pulled from xml -->
                <a href="{link}" target="_blank"><xsl:value-of select="title"/></a>
              </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="movieID"/>
              </td>
			 </tr>
			<xsl:if/>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
  
  
  
  
</xsl:stylesheet>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

<xsl:if/>应该是</xsl:if>,因为它是结束标记而不是自引用标记。

以下是更正后的代码:

&#13;
&#13;
<?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>
	<!-- Background image -->
      <body background="bgimage.jpg">
		
        <h2 style="color:#47B2B2">My Movie Collection</h2>
        <!-- set border, color, and padding-->
        <table border="1" bgcolor="#0A1A1A" cellpadding="5">
          <tr bgcolor="#1F4C4C">
		  <!-- Set order -->
            <th>Title</th>
            <th>Director</th>
			<th>Year</th>
			<th>Genre</th>
			<th>ID</th>
          </tr>
          <xsl:for-each select="movies/movie">
		  <!-- Sort by title -->
            <xsl:sort select="title"/>
			<xsl:if test="year &gt; 2005">
            <tr bgcolor="#3D9999">
			
			  <td>
			  	<!-- Look for link, target to blank, the link text is the tittle pulled from xml -->
                <a href="{link}" target="_blank"><xsl:value-of select="title"/></a>
              </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="movieID"/>
              </td>
			 </tr>
			</xsl:if>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
  
  
  
  
</xsl:stylesheet>
&#13;
&#13;
&#13;