在XSLT中按选择框排序

时间:2010-10-28 04:49:19

标签: xslt sorting

我在我当前正在构建的网站上有这个选择框,我想要做的是按照所选的值对页面上显示的项目列表进行排序。 问题是我有不同类型的数据类型,例如位置和价格。 如果我想对位置进行排序,没有问题,它会根据位置名称的字母顺序进行完美排序。但是,如果我按照“低预算”(从低到高的价格)排序,我必须在排序中添加其他标签(data-type =“number”)。这工作正常,但然后位置等不再工作。

如何解决这个问题,使其处理各种不同的数据类型?

我的XSLT:

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

<xsl:param name="url-filter" select="'recommended'" />

<xsl:template name="resort-list" match="data/resorts/entry">
    <form name="sort-form" action="{$root}/resorts/" method="get">
            <select name="filter" onChange="document.getElementById('sort-form').submit();">
                <option value="">Sort By...</option>
                <option value="recommended">Recommended</option>
                <option value="location">Location</option>
                <option value="prices-from">Low budget</option>
                <option value="prices-till">High budget</option>
            </select>
    </form>

      <xsl:for-each select="data/resorts/entry">
         <!-- this is were I sort the items --> 
         <xsl:sort select="*[name() = $url-filter]" />
        <a href="{$root}/koh-lipe-resorts/resort-view/{resort-name/@handle}">
                    <h3 class="resort-item-heading grey"><xsl:value-of select="resort-name"/></h3>
                    <p> 
                        <xsl:call-template name="truncate">
                            <xsl:with-param name="value" select="resort-description" />
                            <xsl:with-param name="length" select="150" />
                        </xsl:call-template>
                    </p>
        </a>
     </xsl:for-each>
</xsl:template>

<xsl:include href="../utilities/master.xsl"/>
</xsl:stylesheet>

我的XML:

<data>
    <events />
    <resorts>
        <pagination total-entries="11" total-pages="2"
        entries-per-page="10" current-page="1" />
        <section id="9" handle="resorts">Resorts</section>
        <entry id="114">
            <price-from handle="1200">1200</price-from>
            <price-till handle="1900">1900</price-till>
            <recommended>No</recommended>
            <lipe-green-aware-resort>No</lipe-green-aware-resort>
            <name>Baja Resort</name>
            <location>Sunrise Beach</location>
        </entry>
    </resorts>
</data>

2 个答案:

答案 0 :(得分:2)

此转换正确排序每个可能的参数值"recommended""price-from"

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

 <xsl:param name="url-filter" select="'price-from'" />

 <xsl:variable name="vSortOrder">
  <xsl:choose>
   <xsl:when test="$url-filter = 'price-from'">
    <xsl:text>ascending</xsl:text>
   </xsl:when>
   <xsl:otherwise>descending</xsl:otherwise>
  </xsl:choose>
 </xsl:variable>

 <xsl:variable name="vSortType">
  <xsl:choose>
   <xsl:when test="$url-filter = 'price-from'">
    <xsl:text>number</xsl:text>
   </xsl:when>
   <xsl:otherwise>text</xsl:otherwise>
  </xsl:choose>
 </xsl:variable>

 <xsl:template name="resort-list" match="data/resorts">
    <form name="sort-form" action="/*/resorts/" method="get">
            <select name="filter" onChange="document.getElementById('sort-form').submit();">
                <option value="">Sort By...</option>
                <option value="recommended">Recommended</option>
                <option value="location">Location</option>
                <option value="prices-from">Low budget</option>
                <option value="prices-till">High budget</option>
            </select>
    </form>

      <xsl:for-each select="entry">
         <!-- this is were I sort the items -->
         <xsl:sort select="*[name() = $url-filter]"
          data-type="{$vSortType}" order="{$vSortOrder}" />
        <a href="/*/koh-lipe-resorts/resort-view/{resort-name/@handle}">
                    <h3 class="resort-item-heading grey"><xsl:value-of select="name"/></h3>
        </a>
     </xsl:for-each>
 </xsl:template>

 <xsl:template match="/">
  <html>
   <xsl:apply-templates/>
  </html>
 </xsl:template>
</xsl:stylesheet>

使用此XML文档(扩展仅提供一个条目):

<data>
    <events />
    <resorts>
        <pagination total-entries="11" total-pages="2"
        entries-per-page="10" current-page="1" />
        <section id="9" handle="resorts">Resorts</section>
        <entry id="114">
            <price-from handle="1200">1400</price-from>
            <price-till handle="1900">1900</price-till>
            <recommended>No</recommended>
            <lipe-green-aware-resort>No</lipe-green-aware-resort>
            <name>Baja Resort</name>
            <location>Sunrise Beach</location>
        </entry>
        <entry id="115">
            <price-from handle="1500">1500</price-from>
            <price-till handle="1700">1700</price-till>
            <recommended>Yes</recommended>
            <lipe-green-aware-resort>No</lipe-green-aware-resort>
            <name>Blah-Blah Resort</name>
            <location>Blah-Blah Beach</location>
        </entry>
    </resorts>
</data>

并使用$url-filter参数的两个可能值中的每一个,使用正确的排序键数据类型以正确的排序顺序生成正确的结果(升序或降序)。

答案 1 :(得分:1)

好问题。这是一种方法:

  • 为您创建一个命名模板 内部<a>元素,称之为“链接” 例如。
  • 为您的sort-each创建两个命名模板:“sort-by-alpha” 和“按数字排序”。
  • “按数字排序”执行for-each,按编号排序,并调用“链接”模板。
  • “按字母排序”为每个进行排序(按字母排序),然后调用“链接”模板。
  • 在您的外部模板中,当前for-each data/resorts/entry上的prices-from之外的地方,选择何时测试,否则在排序条件时决定调用“按数字排序”是prices-till或{{1}};否则,它会调用sort-by-alpha。

这有意义吗?如果这不适合你,请告诉我。