我有这个选择框在一个网站上,我目前正在建设和我想做的是,排序的项目列表显示在页面上的值是被选择的。问题是我得到了不同类型的数据,例如位置和价格。如果我想按位置排序,没有问题,它完全按照位置名称的字母顺序排序。但是如果我按照从低到高的“低预算”排序,我必须在排序中添加额外的标签(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>发布于 2010-10-29 00:25:20
此转换为每个可能的参数值:"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 参数的两个可能值中的每一个,以正确的排序关键字数据类型和正确的排序顺序(升序或降序)生成正确的排序结果。
发布于 2010-10-28 19:32:21
问得好。这里有一种方法:
为for -each内部元素创建一个命名模板,将其命名为“example.
<a>”和"sort-by-number".
data/resorts/entry上当前有for-each的位置之外,放置一个choose- when -test-否则当排序条件是prices-from或prices-till时,它决定调用“按编号排序”;否则,它将调用sort-by-alpha.这有意义吗?如果这对你不起作用,请告诉我。
https://stackoverflow.com/questions/4039855
复制相似问题