按数据类型下拉列表顺序对列表进行排序

时间:2012-06-26 04:32:08

标签: xslt drop-down-menu umbraco

如何按照与我的数据类型下拉列表相同的顺序对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辅助和其他费用

2 个答案:

答案 0 :(得分:0)

首先,<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进行测试。

您可能会看到,概念是将所需的排序顺序声明为字符串,然后根据每个节点在该排序字符串中的文本值的位置对我们的节点进行迭代排序。

答案 1 :(得分:0)

你在寻找这样的东西: -

假设你的xml是这样的: -

XML:

<preValues>
    <preValue id="19">Value 1</preValue>
    <preValue id="20">Value 2</preValue>
    <preValue id="21">Value 3</preValue>
</preValues>

XSLT:

如果您想按id顺序按descending排序,请按照这种方式进行排序

<xsl:for-each select="umbraco.library:GetPreValues(1601)//preValue">
    <xsl:sort select="@id" order="descending" />
    <!-- Do Your Stuff --> 
</xsl:for-each>

按照相同的方法,按多个属性值排序。

相关问题