2014-12-07 71 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的輸出。

相關問題