【问题标题】:Sort list by datatype dropdown list order按数据类型下拉列表顺序排序列表
【发布时间】:2012-06-26 04:32:07
【问题描述】:
如何在 XSLT 中按照与数据类型下拉列表相同的顺序对列表进行排序?
我尝试了以下方法但没有成功:
<xsl:variable name="tarrifs" select="umbraco.library:GetPreValues(1601)//preValue" />
<xsl:sort select="tarrifs" order="descending" />
我的数据类型“关税类别”中有一个选项列表,我想按照与我的数据类型相同的顺序在 XSLT 中对列表进行排序。
我的数据类型的顺序:
UE 资费标准
UE 定价建议
UE 资费表
UE 其他费用
UE 资费策略报告
MG 资费表
MG 关税报告
MG 辅助及其他费用
【问题讨论】:
标签:
xslt
drop-down-menu
umbraco
【解决方案1】:
首先,<xsl:sort... 必须出现在 <xsl:apply-templates... 或 <xsl:for-each... 标记内。
要按照您的需要使用主观逻辑进行排序,您可以采用几种方法。最简单的大概是这样的:
XML
<root>
<item>dog</item>
<item>cat</item>
<item>horse</item>
<item>dragonfly</item>
</root>
XSL
<!-- this sheet vars -->
<xsl:variable name='sort_order' select='"dragonfly|horse|dog|cat"' />
<!-- root and static content -->
<xsl:template match="/">
<xsl:apply-templates select='root/item'>
<xsl:sort select='string-length(substring-before($sort_order, current()/text()))' data-type='number' />
</xsl:apply-templates>
</xsl:template>
<!-- iteration content - animal -->
<xsl:template match='item'>
<p><xsl:value-of select='.' /></p>
</xsl:template>
您可以在this XMLPlayground session 进行测试。
您可能会看到,这个概念是将所需的排序顺序声明为一个字符串,然后根据每个节点的文本值在该排序字符串中的位置迭代地对我们的节点进行排序。
【解决方案2】:
您是否正在寻找这样的东西:-
假设你的 xml 是这样的:-
XML:
<preValues>
<preValue id="19">Value 1</preValue>
<preValue id="20">Value 2</preValue>
<preValue id="21">Value 3</preValue>
</preValues>
XSLT:
如果你想在descending 中按id 排序,那么就这样做
<xsl:for-each select="umbraco.library:GetPreValues(1601)//preValue">
<xsl:sort select="@id" order="descending" />
<!-- Do Your Stuff -->
</xsl:for-each>
按照相同的方法按多个属性值排序。