簡體   English   中英

如何在XSL文件中獲取數據

[英]How to get data in xsl file

我有這樣的XML代碼:

    <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經驗,請幫助我解決它

**我希望結果像 這個 **

更改此:

<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> 

要得到:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM