XSLT自定义排序 [英] XSLT Custom Sort
问题描述
在XSLT中是否可以按字母顺序排序,其中5项为首选".
Is it possible in XSLT to sort in alphabetical order, with 5 items as "preferred".
即给
<teams>
<team id="142" name="Scotland" />
<team id="110" name="Liverpool" />
<team id="13" name="Manchester United" />
<team id="123" name="England" />
<team id="84" name="Chelsea" />
<team id="295" name="Wales" />
<team id="49" name="Arsenal" />
<team id="126" name="Northern Ireland" />
<team id="121" name="Republic of Ireland" />
<team id="42" name="Manchester City" />
<team id="298" name="Tottenham Hotspur" />
<team id="299" name="Bolton" />
</teams>
我要求国家队按照特定的顺序排在第一位,然后其余部分按照字母顺序排在第二位:
I require the national teams to be sorted first in a certain order, followed by the rest in alphabetical order:
<teams>
<team id="123" name="England" />
<team id="126" name="Northern Ireland" />
<team id="121" name="Republic of Ireland" />
<team id="142" name="Scotland" />
<team id="295" name="Wales" />
<team id="49" name="Arsenal" />
<team id="299" name="Bolton" />
<team id="84" name="Chelsea" />
<team id="110" name="Liverpool" />
<team id="42" name="Manchester City" />
<team id="13" name="Manchester United" />
<team id="298" name="Tottenham Hotspur" />
</teams>
我一直在尝试,但是失败了.
I have been trying, but failing.
是否有一种整齐的方法来做到这一点,或者您必须对国家队进行单独排序,然后再将所有国家队排除在外?
Is there a neat way to do this, or do you have to sort the national teams individually, followed by a sort which excludes all the national teams?
推荐答案
您可以执行以下操作:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://tempuri.org"
exclude-result-prefixes="my"
>
<xsl:output method="xml" indent="yes" />
<my:data>
<my:nationalteams>
<my:team id="121" /><!-- Republic of Ireland -->
<my:team id="123" /><!-- England -->
<my:team id="126" /><!-- Northern Ireland -->
<my:team id="142" /><!-- Scotland -->
<my:team id="295" /><!-- Wales -->
</my:nationalteams>
</my:data>
<xsl:template match="teams">
<xsl:copy>
<xsl:variable name="national" select="
document('')/*/my:data/my:nationalteams/my:team
" />
<!-- national teams preferred -->
<xsl:apply-templates select="team[@id = $national/@id]">
<xsl:sort select="@name" />
</xsl:apply-templates>
<!-- other teams after them -->
<xsl:apply-templates select="team[not(@id = $national/@id)]">
<xsl:sort select="@name" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="team">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
整个<my:data>
可以移动到辅助XML/config文件中,您也可以在其中省略我的"命名空间.
The whole <my:data>
could be moved to a secondary XML/config file, where you can also leave off the "my" namespace.
在那之后,一行需要做一点改动:
After that, one line would need a small change:
<xsl:variable name="national" select="
document('config.xml')/data/nationalteams/team
" />
上面的输出有点令人惊讶:-)
The output of the above is somewhat unsurprising :-)
<teams>
<team id="123" name="England" />
<team id="126" name="Northern Ireland" />
<team id="121" name="Republic of Ireland" />
<team id="142" name="Scotland" />
<team id="295" name="Wales" />
<team id="49" name="Arsenal" />
<team id="299" name="Bolton" />
<team id="84" name="Chelsea" />
<team id="110" name="Liverpool" />
<team id="42" name="Manchester City" />
<team id="13" name="Manchester United" />
<team id="298" name="Tottenham Hotspur" />
</teams>
这篇关于XSLT自定义排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!