如何按版本对列表进行排序

时间:2018-11-30 21:59:13

标签: xml sorting xslt version xslt-1.0

是否可以使用xslt对版本号进行排序,而不会涉及怪异的计算和字符串操作?

我有一个看起来像这样的列表:

<?xml version="1.0"?>
<list>
  <directory mtime="2018-08-23T09:40:02Z">2.12.01</directory>
  <directory mtime="2018-11-19T15:04:06Z">2.12.02</directory>
  <directory mtime="2018-09-05T14:07:04Z">3.0.0</directory>
  <directory mtime="2018-10-08T07:14:02Z">3.0.1</directory>
  <directory mtime="2018-11-29T17:06:58Z">3.0.12</directory>
  <directory mtime="2018-11-13T11:34:26Z">2.99.99</directory>
  <directory mtime="2018-11-29T17:06:58Z">latest</directory>
</list>

我想按版本号对它进行排序,以使3.0.0例如在2.99.99之前。这是我的解决方法,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<html>
<body>
    <table border="0">
    <tr bgcolor="#9acd32">
        <th>name</th>
        <th>date</th>
    </tr>
    <xsl:for-each select="list/*">
        <xsl:sort select="(number(substring-before(., '.')) * 10000) + (number(substring-before(substring(., number(string-length(substring-before(., '.'))+2), 6), '.')) * 10) + (number(substring-after(substring(., number(string-length(substring-before(., '.'))+2), 6), '.')))" data-type="number" order="descending" />

        <xsl:variable name="name">
            <xsl:value-of select="."/>
        </xsl:variable>
        <xsl:variable name="date">
            <xsl:value-of select="substring(@mtime,9,2)"/>-<xsl:value-of select="substring(@mtime,6,2)"/>-<xsl:value-of select="substring(@mtime,1,4)"/><xsl:text> </xsl:text>
            <xsl:value-of select="substring(@mtime,12,2)"/>:<xsl:value-of select="substring(@mtime,15,2)"/>:<xsl:value-of select="substring(@mtime,18,2)"/>
        </xsl:variable>

        <tr>
             <td><a href="{$name}"><xsl:value-of select="."/></a></td>
             <td><xsl:value-of select="$date"/></td>
        </tr>
    </xsl:for-each>
    </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

它的作用是分裂。并将其转换为数字,然后用10000计算第一个数字,用10计算第二个数字,然后将它们加起来成为一个可以排序的数字。

1 个答案:

答案 0 :(得分:1)

这就是我重写您的尝试的方式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>

<xsl:template match="/list">
    <html>
        <body>
            <table>
                <tr>
                    <th>name</th>
                    <th>date</th>
                </tr>
                <xsl:for-each select="directory">
                    <xsl:sort select="substring-before(., '.')" data-type="number" order="descending"/>
                    <xsl:sort select="substring-before(substring-after(., '.'), '.')" data-type="number" order="descending"/>
                    <xsl:sort select="substring-after(substring-after(., '.'), '.')" data-type="number" order="descending"/>
                    <tr>
                        <td>
                            <a href=".">
                                <xsl:value-of select="."/>
                            </a>
                        </td>
                        <td>
                            <xsl:value-of select="substring(@mtime, 9, 2)"/>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="substring(@mtime, 6, 2)"/>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="substring(@mtime, 1, 4)"/>
                            <xsl:text> </xsl:text>
                            <xsl:value-of select="substring(@mtime, 12, 8)"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html> 
</xsl:template>

</xsl:stylesheet>