xsl如何应用模板正确

时间:2015-12-23 10:44:39

标签: xml xslt

如果我们考虑使用html,我希望为内容数据管理2 div。1 is list data2 is for summary。喜欢这张图片

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。 (我制作布局很愚蠢。)

1 个答案:

答案 0 :(得分: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> 

我已经猜到了如何区分标准酒店和豪华酒店客房,但它应该证明原则。

相关问题