按上次修改日期对站点地图 URL 进行排序

Sort Sitemap URLs By Last Modified Date

我有一个像这样的 XML 1.0 站点地图:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://localhost/page-1</loc>
<lastmod>2020-09-21</lastmod>
</url>
<url>
<loc>http://localhost/page-2</loc>
<lastmod>2020-09-23</lastmod>
</url>
</urlset>

这是上述 XML 站点地图的 XSLT 1.0 样式表:

<xsl:template match="sm:urlset">
<table>
<thead>
<tr>
<th></th>
<th>URL</th>
<th>Last Modified</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="sm:url">
<tr>
<xsl:variable name="pos">
<xsl:value-of select="position()" />
</xsl:variable>
<xsl:variable name="loc">
<xsl:value-of select="sm:loc" />
</xsl:variable>
<xsl:variable name="lastmod">
<xsl:value-of select="sm:lastmod" />
</xsl:variable>
<th>
<xsl:value-of select="$pos" />
</th>
<td>
<xsl:value-of select="$loc" />
</td>
<td>
<xsl:value-of select="$lastmod" />
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>

如何按 sm:lastmod(最后修改)日期对站点地图进行排序?请注意,我目前使用的是 Microsoft Edge 版本 87.0.664.60(基于 Chromium 嵌入式框架)。

XSLT 3 和更高阶函数(Saxon 10 所有版本、Saxon 9.8 和 9.9 PE 和 EE、Saxon JS 2)支持最紧凑的是 xsl:for-each select="sort(sm:url, (), function($url) { $url/sm:lastmod })"

否则当然在for-each里面使用xsl:sort<xsl:sort select="sm:lastmod"/>.