2015-07-03 133 views
0

我試圖循環所需要的表裏面印有數據元素 這裏是我的xml文件XSL爲每一個元素

 <hotels xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
    xsi:noNamespaceSchemaLocation='5.xsd'> 
<hotel ID="1"> 
    <Name>Les Jardins du Marais</Name> 
    <Stars>3</Stars> 
    <Facilities>Internet</Facilities> 
    <Address>74 reu Amelot, Paris, 75011</Address> 
    <Distance>2</Distance> 
    <Available>true</Available> 
</hotel> 
<hotel ID="2"> 
    <Name>Golden Tulip Little Palace</Name> 
    <Stars>4</Stars> 
    <Facilities>Internet</Facilities> 
    <Facilities>Gym</Facilities> 
    <Facilities>Parking</Facilities> 
    <Facilities>Restaurant</Facilities> 
    <Address>4 rue salomo,De caus, Paris, 75003</Address> 
    <Distance>0.1</Distance> 
    <Available>false</Available> 
</hotel> 
<hotel ID="3"> 
    <Name>Tilsitt Etoile</Name> 
    <Stars>2</Stars> 
    <Facilities>Restaurant</Facilities> 
    <Address>23 rue brey, Paris, 75017</Address> 
    <Distance>3</Distance> 
    <Available>false</Available> 
</hotel> 
<hotel ID="4"> 
    <Name>Hotel saint charles</Name> 
    <Stars>3</Stars> 
    <Facilities>Parking</Facilities> 
    <Address>6 rue de 1'Esperance, Paris, 75013</Address> 
    <Distance>1</Distance> 
    <Available>true</Available> 
</hotel> 

這裏是我的XSL文件

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="html"/> 
<xsl:template match="/"> 
    <html> 
     <body> 
      <h2>Students</h2> 
      <table border="1"> 
       <tr bgcolor="#9acd32"> 
        <th>ID</th> 
        <th>Name</th> 
        <th>Stars</th> 
        <th>Facilites</th> 
        <th>Address</th> 
        <th>Distance</th> 
        <th>Available</th>  
       </tr> 
       <xsl:for-each select="hotels/*"> 
        <tr>  
         <td> 
          <xsl:value-of select="@ID"/> 
         </td> 
         <td> 
          <xsl:value-of select="Name"/> 
         </td> 
         <td> 
          <xsl:value-of select="Stars"/> 
         </td> 
         <td> 
          <xsl:value-of select="Facilities"/> 
         </td> 
         <td> 
          <xsl:value-of select="Address"/> 
         </td> 
         <td> 
          <xsl:value-of select="Distance"/> 
         </td> 
         <td> 
          <xsl:value-of select="Available"/> 
         </td>  
        </tr> 
       </xsl:for-each> 
      </table> 
     </body> 
    </html> 
</xsl:template> 

由於有多個單一酒店我需要fi發出如何循環該設施,任何幫助將graet並提前感謝

+0

請張貼預期的結果。 –

回答

0

雖然你可以添加第二個,嵌套xsl:foreach,通常最好保持Xsl分解成較小的模板,其中每個是重點關注具體問題。最好使用模板匹配,而不是命令循環來處理你的XML,因爲它允許過濾,遞歸和其他好處。通過這種方式,可以爲每個Facility定義一個模板,然後您可以從酒店模板中應用它。您還需要確定要如何顯示在您的表的設施名單 - 我認爲嵌套表在這裏:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html"/> 
    <xsl:template match="/"> 
     <html> 
      <body> 
       <h2>Students</h2> 
       <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th>ID</th> 
         <th>Name</th> 
         <th>Stars</th> 
         <th>Facilites</th> 
         <th>Address</th> 
         <th>Distance</th> 
         <th>Available</th> 
        </tr> 
        <xsl:apply-templates select="hotels/hotel" /> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="hotel"> 
     <tr> 
      <td> 
       <xsl:value-of select="@ID"/> 
      </td> 
      <td> 
       <xsl:value-of select="Name"/> 
      </td> 
      <td> 
       <xsl:value-of select="Stars"/> 
      </td> 
      <td> 
       <table> 
        <xsl:apply-templates select="Facilities" /> 
       </table> 
      </td> 
      <td> 
       <xsl:value-of select="Address"/> 
      </td> 
      <td> 
       <xsl:value-of select="Distance"/> 
      </td> 
      <td> 
       <xsl:value-of select="Available"/> 
      </td> 
     </tr> 
    </xsl:template> 

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

</xsl:stylesheet> 

Xsl Transform fiddle