2015-12-21 41 views
0

數據我有XML這樣的代碼:如何獲得在XSL文件

<hotels> 
    <hotel contact="(855) 23 430 732"> 
     <name>hotel A</name> 
     <type>hotel or apartment</type> 
     <rating>1 to 5</rating> 
     <address> 
      <houseNo>73 </houseNo> 
      <street>Preah Monivong Blvd </street> 
      <Sangkat>Mean Chey</Sangkat > 
      <Khan>Daun Penh</Khan> 
      <city>Bangkok</city> 
     </address> 
     <room> 
      <roomType>hotel: standard/deluxe, apartment: studio/2-bedroom/3-bedroom</roomType> 
      <price>209</price> 
      <description>Descriptions</description> 
     </room> 
     <room> 
      <roomType>hotel: standard/deluxe, apartment: studio/2-bedroom/3-bedroom</roomType> 
      <price>210</price> 
      <description>Descriptions</description> 
     </room> 
     <room> 
      ..... 
     </room> 
    </hotel> 
    <hotel contact="..."> 
     ... ... 
     ... ... 
    </hotel> 
</hotels> 

使用xsl代碼我該怎麼讀呢?這是我的代碼:

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

    <xsl:template match="/"> 
    <html> 
    <body> 
    <h2>test xsl</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
      <th style="text-align:left;">Name</th> 
      <th style="text-align:left;">Type</th> 
      <th style="text-align:left;">Rating</th> 
      <th style="text-align:left;">Address</th> 
      <th style="text-align:left;">Room</th> 
     </tr> 
     <xsl:for-each select="hotels/hotel"> 
     <tr> 
     <!--<xsl:value-of select="."/>--> 
      <td><xsl:value-of select="name" /></td> 
      <td><xsl:value-of select="type" /></td> 
      <td><xsl:value-of select="rating" /></td> 
      <td><xsl:value-of select="address" /></td> 
      <td><xsl:value-of select="room" /></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

問題我的代碼是什麼錯誤,但對於<room>可以只加載的第一個孩子。任何一個有經驗的xsl請幫我解決這個問題

**我希望結果一樣this **

+0

請張貼一個完整的樣式表**和**預期的結果。 - **提示**:您需要使用另一個'xsl:for-each'或'xsl:apply-templates'來處理房間。 –

+0

請再次檢查,我編輯準備 – Songs

+0

我仍然沒有看到預期的結果。您正在爲每家酒店創建一個行:您如何期望在單行中顯示多個房間? –

回答

1

更改此:

<td><xsl:value-of select="room" /></td> 

到:

<td> 
    <xsl:for-each select="room"> 
     <xsl:text>- </xsl:text> 
     <xsl:value-of select="roomType" /> 
     <xsl:text> Price: </xsl:text> 
     <xsl:value-of select="price" /> 
     <xsl:text> Description: </xsl:text> 
     <xsl:value-of select="description" /> 
     <br/> 
    </xsl:for-each> 
</td> 

個人而言,我寧願做類似的事情:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/hotels"> 
    <html> 
     <body> 
      <h2>test xsl</h2> 
      <table border="1"> 
       <tr bgcolor="#9acd32"> 
        <th>Name</th> 
        <th>Type</th> 
        <th>Rating</th> 
        <th>Address</th> 
        <th>Room Type</th> 
        <th>Room Price</th> 
        <th>Description</th> 
       </tr> 
       <xsl:apply-templates select="hotel"/> 
      </table> 
     </body> 
    </html> 
</xsl:template> 

<xsl:template match="hotel"> 
    <xsl:variable name="rowspan" select="count(room) + 1" /> 
    <tr> 
     <td rowspan="{$rowspan}"><xsl:value-of select="name"/></td> 
     <td rowspan="{$rowspan}"><xsl:value-of select="type"/></td> 
     <td rowspan="{$rowspan}"><xsl:value-of select="rating"/></td> 
     <td rowspan="{$rowspan}"><xsl:value-of select="address"/></td> 
    </tr> 
    <xsl:apply-templates select="room"/> 
</xsl:template> 

<xsl:template match="room"> 
    <tr> 
     <td><xsl:value-of select="roomType"/></td> 
     <td><xsl:value-of select="price"/></td> 
     <td><xsl:value-of select="description"/></td> 
    </tr> 
</xsl:template> 

</xsl:stylesheet> 

獲得:

enter image description here

+0

它的工作,謝謝。 – Songs

+0

我可否在''中知道'var_dump'作爲'PHP',還是不知道我們加載的視圖數據。我真的不知道如何調試它。 – Songs

+0

@歌曲我不明白你在問什麼。這似乎與這個問題或一般的XSLT無關。我建議你開始一個新的問題。 –