Sort XML with XSL

20 October 2012

Keywords: xsl, sort, merge, feed

Starting with version 1.2, etc_query supports XSL stylesheets. An immediate benefice is the possibility to sort an XML document. In the example below we mix and rearrange by date <txp:article_custom /> and <txp:recent_comments /> lists:

<txp:etc_query
	data='<dl>
	<txp:article_custom>
		<dt><txp:posted format="%F %T" /></dt>
		<dd>Published <txp:permlink><txp:title /></txp:permlink></dd>
	</txp:article_custom>
	<txp:recent_comments break="">
		<dt><txp:comment_time format="%F %T" /></dt>
		<dd>Comment on <txp:comment_permlink><txp:title /></txp:comment_permlink> by <txp:comment_name /></dd>
	</txp:recent_comments>
	</dl>'
>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:template match="dl">
	<h4>History</h4>
	<dl>
	<xsl:for-each select="dt">
		<xsl:sort select="." order="descending" />
		<xsl:if test="position() &lt; 11">
		<xsl:copy-of select="." />
		<xsl:copy-of select="following-sibling::dd[1]" />
		</xsl:if>
	</xsl:for-each>
	</dl>
</xsl:template>
</xsl:stylesheet>
</txp:etc_query>

History (page 1)

2020-05-21 15:06:04
Comment on Conditional tags by Oleg
2020-05-19 21:50:05
Comment on Conditional tags by marios
2020-01-18 13:54:11
Comment on Babylon by Oleg
2020-01-18 13:31:48
Comment on Babylon by Pat64
2020-01-17 23:14:11
Comment on A book by Oleg
2020-01-17 23:06:25
Comment on Babylon by Oleg
2020-01-17 23:02:48
Published Hamlet
2020-01-17 22:55:38
Published Babylon
2020-01-17 22:50:30
Published Гамлет
2020-01-17 22:39:27
Published Hamlet

But what about pagination? Here it is:

File(s)