xsl:for-each,其值不产生输出

时间:2015-12-08 20:53:17

标签: xml xslt foreach

所以我有一个简单的xml和一些电影。我正在尝试执行一些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></head><body>
	
	<table border="1" cellspacing="0" cellpadding="10">
	<th style="background-color:lightgray">Title</th>
	<th style="background-color:lightgray">Director</th>
	<th style="background-color:lightgray">Genre</th>
	
	<xsl:for-each select="movies/movie">
	<tr>
		<td>
			<xsl:value-of select"title">
		</td>
		
		<td>
			<xsl:value-of select"director">
		</td>
		
		<td>
			<xsl:for-each select="genre">
				  
					<xsl:value-of select="genre"/>
				  
			</xsl:for-each>
		</td>
	</tr>	
	</xsl:for-each>	
	
	</table>
	</body></html></xsl:template>
	</xsl:stylesheet>

这是xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Movies2.xslt"?>
  
<movies>
	
	<!-- 1 -->
    <movie>      
           
        <title>Schindler's List</title>
        <director>Steven Spielberg</director>  
        <genre>Biography, </genre>
		<genre>Drama, </genre>
		<genre>History</genre>
	
    </movie>
 
</movies> 

1 个答案:

答案 0 :(得分:1)

我看到2个问题:

  1. 您的xsl:value-of个元素中有两个格式不正确。您需要关闭它们,select属性应该有=
  2. 在内部xsl:for-each中,您选择genre,这样就是新的上下文。 select应为.which selects the context node)。
  3. 示例(也可以在这里看http://xsltransform.net/94rmq6m):

    <xsl:for-each select="movies/movie">
      <tr>
        <td>
          <xsl:value-of select="title"/>
        </td>
    
        <td>
          <xsl:value-of select="director"/>
        </td>
    
        <td>
          <xsl:for-each select="genre">
    
            <xsl:value-of select="."/>
    
          </xsl:for-each>
        </td>
      </tr> 
    </xsl:for-each> 
    
相关问题