如何使用XSL显示有序结果?

时间:2013-12-10 15:51:25

标签: xml xslt

XSL新手,需要帮助创建XSL以显示名称,描述和start_date,结果按开始日期的顺序出现(start_date的结果早于其他人需要首先出现的结果,然后是下一个日期结果,依此类推)将当前日期考虑在内,这意味着不应显示start_date已过去的结果。对于以下XML

 <?xml version="1.0" encoding="UTF-8"?>
 <search-results>
<size>9</size>
<items>
    <data-item id="224">
        <name>Meeting</name>
        <version>35</version>
        <description>Meeting with foreign delegates</description>
        <keywords>English Event</keywords>
        <metadata>
            <item>
                <name>Meeting</name>
                <description>Meeting with foreign delegates</description>
                <keywords>English Event</keywords>
                <locale id="ar_en">English</locale>
                <title>English Event 3</title>
                <summary>English Event 3</summary>
                <description>English Event 3</description>
                <venue>nkjln</venue>
                <evnt_type>exhibitions</evnt_type>
                <start_date timestamp="1387324800000">1.2.2014</start_date>
                <enddate timestamp="1387929600000">5.2.2014</enddate>
                <status>approved,draft</status>
            </item>
        </metadata>
    </data-item>
    <data-item id="498">
        <name>English Event 777</name>
        <version>10</version>
        <description>Special event</description>
        <keywords />
        <metadata>
            <item>
                <name>English Event 777</name>
                <description>Special event</description>
                <keywords />
                <locale id="ar_en">English</locale>
                <title>English Event 777</title>
                <summary>English Event 777</summary>
                <description>Event 5</description>
                <venue>Opera House</venue>
                <evnt_type>national</evnt_type>
                <start_date timestamp="1387929600000">13.3.2014</start_date>
                <enddate timestamp="1388448000000">15.3.2014</enddate>
                <status>approved,draft</status>
            </item>
        </metadata>
    </data-item>
 </items>
 </search-results>

1 个答案:

答案 0 :(得分:0)

您需要使用当前日期将参数传递给此XSLT模板:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="current-date" select="1387324800000" />
    <xsl:template match="/search-results">
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Start date</th>
                </tr>
            </thead>
            <tbody>
                <xsl:apply-templates select="items/data-item/metadata/item[start_date/@timestamp &gt; $current-date]">
                    <xsl:sort select="start_date/@timestamp" data-type="number" />
                </xsl:apply-templates>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template match="item">
        <tr>
            <td><xsl:value-of select="name"        /></td>
            <td><xsl:value-of select="description" /></td>
            <td><xsl:value-of select="start_date"  /></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>