2015-11-05 51 views
-1

我有一个.xml文件,我应该用XSL转换成html文件。XSL:“为每个选择”功能不能正常工作

我的XML:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="test.xsl" ?> 
<Company> 

<SectionA> 
    <Employee>Peter Barry</Employee> 
    <Employee>Lisa Stewart</Employee> 
    <Employee>Harry Rogers</Employee> 
</SectionA> 

<SectionB> 
    <Employee>Tom Riddle</Employee> 
</SectionB> 

</Company> 

在我的HTML文件的输出应该是这样的: “彼得·巴里,丽莎·斯图尔特,哈利·罗杰斯”。

问题是for-each功能在这种情况下不起作用! 我的XSL代码:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
<html> 
<body> 

<h2>All</h2> 

<table> 

<td> 
    <xsl:for-each select="Company/SectionA"> 
    <xsl:value-of select="Employee"/> 
    </xsl:for-each> 
</td> 


</table> 


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

</xsl:stylesheet> 

在HTML它只显示第一个雇员的名字(即“彼得·巴里”)。我如何才能做到这一点,以展示每一个元素?

+0

请出示一个** **重复性例子。我们不知道你的代码是干什么的,不知道它在哪个上下文中执行。也就是说,一个空的'xsl:for-each'不会做任何事情。把东西放进去,例如'XSL:价值of'。 - 另外,请将您的预期输出**显示为代码**。 –

+0

对不起,我现在把这两个代码现在写入我的文章! –

+0

顺便说一句,'​​'元素需要位于''内。 – Flynn1179

回答

0

如果你想在A部分每个员工一排,比使用:

<xsl:template match="/"> 
    <table> 
     <xsl:for-each select="Company/SectionA/Employee"> 
      <tr><td><xsl:value-of select="."/></td></tr> 
     </xsl:for-each> 
    </table> 
</xsl:template> 

你现在的样子,你是在SectionA<xsl:value-of select="Employee"/>上下文返回第一的价值仅儿童员工 - 这就是XSLT 1.0中的工作原理。另外,您只创建一个表格单元格而没有行。

1

使用的for-each是不是在这种情况下,最好的选择,这将是更好地定义一个模板来处理每一个员工,像这样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <h2>All</h2> 
     <xsl:apply-templates select="Company/SectionA"/> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="SectionA"> 
    <table> 
     <xsl:apply-templates/> 
    </table> 
    </xsl:template> 

    <xsl:template match="Employee"> 
    <tr> 
     <td><xsl:value-of select="."/></td> 
    </tr> 
    </xsl:template>   
</xsl:stylesheet>