2015-12-23 23 views
0

如果我們考慮html,我想管理2 div的內容數據。 1 is list data2 is for summary。這樣的形象xsl如何申請 - 模板正確

enter image description here

xsl,這是我的代碼

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

<xsl:template match="/hotels"> 
<html> 
    <body> 
     <h2>Transform 2</h2> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
       <th>Name</th> 
       <th>Type</th> 
       <th>Rating</th> 
       <th>Address</th> 
      </tr> 
      <xsl:apply-templates select="hotel[rating >= 4]"/> 
     </table> 
    </body> 
</html> 
</xsl:template> 

<xsl:template match="hotel"> 
<tr> 
    <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> 
</tr> 
</xsl:template> 
</xsl:stylesheet> 

而且我XML代碼

<hotels> 
    <hotel contact="(855) 23 430 732"> 
     <name>hotel A</name> 
     <type>hotel</type> 
     <rating>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> 
    </hotel> 
    <hotel> 
    ..... 
    </hotel> 

我的問題: 我怎麼能寫second templates內容摘要數據。 (我愚蠢的做佈局。)

+0

那麼是什麼問題,兩次處理同樣的節點?在你的第二個模板和'apply-templates'上使用'mode'。或者是計算總量和平均值的問題?您顯示的示例中有像' hotel:standard/deluxe'這樣的數據,我不確定代碼如何確定某個房間是屬於「標準」還是「豪華」類別,您似乎希望這樣做你的總結。 –

回答

0

你不一定需要第二個模板的摘要。您可以將所需的代碼放入與hotels匹配的現有模板中。您將利用countsum函數提供摘要。例如

<table> 
     <tr> 
      <td>Number of Standard Rooms</td> 
      <td><xsl:value-of select="count(hotel[rating >= 4]/room)"/></td> 
     </tr> 
     <tr> 
      <td>Average Cost of Standard Rooms</td> 
      <td><xsl:value-of select="sum(hotel[rating >= 4]/room/price) div count(hotel[rating >= 4]/room)"/></td> 
     </tr> 
    </table> 

但是,它看起來像你想爲「標準」和「豪華」房獨立的摘要,雖然你還沒有真正說如何區分它們。假設有一個方法,你可以使用一個命名模板,以避免重複代碼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/hotels"> 
<html> 
    <body> 
     <h2>Transform 2</h2> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
       <th>Name</th> 
       <th>Type</th> 
       <th>Rating</th> 
       <th>Address</th> 
      </tr> 
      <xsl:apply-templates select="hotel[rating >= 4]"/> 
     </table> 
     <table> 
      <xsl:call-template name="summary"> 
       <xsl:with-param name="type">Deluxe</xsl:with-param> 
       <xsl:with-param name="rooms" select="hotel/room[starts-with(roomType, 'hotel: standard/deluxe,')]" /> 
      </xsl:call-template> 
      <xsl:call-template name="summary"> 
       <xsl:with-param name="type">Deluxe</xsl:with-param> 
       <xsl:with-param name="rooms" select="hotel/room[starts-with(roomType, 'hotel: standard,')]" /> 
      </xsl:call-template> 
     </table> 
    </body> 
</html> 
</xsl:template> 

<xsl:template match="hotel"> 
<tr> 
    <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> 
</tr> 
</xsl:template> 

<xsl:template name="summary"> 
    <xsl:param name="type" /> 
    <xsl:param name="rooms" /> 
    <xsl:if test="$rooms"> 
     <tr> 
      <td>Number of <xsl:value-of select="$type" /> Rooms</td> 
      <td><xsl:value-of select="count($rooms)"/></td> 
     </tr> 
     <tr> 
      <td>Average Cost of <xsl:value-of select="$type" /> Rooms</td> 
      <td><xsl:value-of select="sum($rooms/price) div count($rooms)"/></td> 
     </tr> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

我做了一個猜測,如何標準間和豪華酒店房間進行區分,但要表現的原則。