【问题标题】:IF on xlst not workingIF 在 xslt 中不起作用
【发布时间】:2015-10-23 19:05:02
【问题描述】:

在过去的一个小时里,我一直在用头撞墙,试图弄清楚为什么 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>

【问题讨论】:

  • 你需要一个更好的测试工具,它可以告诉你哪里出了问题。
  • @michael.hor257k 你推荐哪一个?
  • 这取决于您的需求和平台。至少,使用像样的在线工具,例如xsltransform.net
  • @michael.hor257k 遵循了它所说的建议:xsl 错误:第 26 行第 33 列 XTSE0010 上的排序错误:xsl:if 元素不得包含 xsl:sort 元素
  • 在您发布的样式表中,xsl:sort (正确地)是 xsl:for-each 的第一个孩子 - 所以您描述的问题不会发生。

标签: xml xslt if-statement logic


【解决方案1】:

&lt;xsl:if/&gt; 应该是&lt;/xsl:if&gt;,因为它是一个结束标签而不是自引用标签。

以下是更正后的代码:

<?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>

【讨论】:

  • &lt;xsl:if test="year&gt;2005"&gt; 没有任何问题。唯一的问题是自动关闭&lt;xsl:if/&gt;
  • @michael.hor257k 我已经在我的电脑上尝试了 w3 的测试核心,它需要关闭 if
  • @user3541786 它需要一个关闭的&lt;/xsl:if&gt;,而不是一个自关闭 &lt;xsl:if/&gt;,它是&lt;xsl:if&gt;&lt;/xsl:if&gt; 的缩写(一个空的xsl:if 元素)。你现在拥有它的方式,你有一个打开的&lt;xsl:if&gt;标签,它永远不会关闭。
  • stackoverflow.com/questions/9097550/… 会支持我的情况。
  • @user3541786 如果您针对已链接到的输入进行测试,它什么也做不了。因为&lt;xsl:for-each select="movies/movie"&gt; 在输入有catalog/cd 时什么都不选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多