2014-12-07 74 views
0

我的文档是:XML“xsl:for-each”项目的相同名称?

<?xml version="1.0" encoding="UTF-8" ?> 
    <?xml-stylesheet type="text/xsl" href="university_style.xsl"?> 
    <!DOCTYPE library SYSTEM "validator.dtd"> 
     <university> 
      <total_faculty>13</total_faculty> 
      <faculty> 
       <id>1</id> 
       <name>name 1</name> 
       <total_chairs>9</total_chairs> 
       <chairs_list> 
        <chair>name 1</chair> 
        <chair>name 2</chair> 
        <chair>name 3</chair> 
    ... 
       </chairs_list> 
      </faculty> 

     </university> 

和XSL

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
<body> 
     <table border="1" cellpadding="4" cellspacing="0"> 
     <caption>total_faculty:<xsl:value-of select="university/total_faculty"/></caption> 
     <tr bgcolor="#999999" align="center"> 
      <th>id</th> 
      <th>name</th> 
      <th>total chairs</th> 
      <th>chairs</th> 
     </tr> 
     <xsl:for-each select="university/faculty"> 
      <tr> 
      <td> 
       <xsl:value-of select="id"/> 
      </td> 
      <td> 
       <xsl:value-of select="name"/> 
      </td> 
      <td> 
       <xsl:value-of select="total_chairs"/> 
      </td> 
      <td> 
       <!--<p><xsl:value-of select="chairs_list"/></p> --> 
       <xsl:for-each select="chairs_list"> 
       <p><xsl:value-of select="chair"/> </p> 
       </xsl:for-each> 
      </td> 
      </tr> 
     </xsl:for-each> 
     </table> 
</body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

我要显示在新行(

椅子

)的所有元素。 但我看到第一个元素或全部。如果一个使用全部列在一行中。

如果我使用:

<xsl:for-each select="chairs_list"> 
    <p><xsl:value-of select="chair"/> </p> 
</xsl:for-each> 

我看到名单只是第一个元素。如何解决它? :)

回答

1

只要改变你的xsl:for-each

<xsl:for-each select="chairs_list/chair"> 
    <p><xsl:value-of select="."/></p> 
</xsl:for-each> 

结果:

<p>name 1</p> 
<p>name 2</p> 
<p>name 3</p> 

这种调整for-each选择所有chair元素在chairs_list,循环遍历它们,并产生作为输出电流的含量节点 - select="." - 此循环。您之前的for-each仅选择了chairs_list,因此<xsl:value-of select="chair"/>仅在此列表中具有第一个chair的输出。

相关问题