SoFunction
Updated on 2025-04-07

Implementing random sorting in XSL/XSLT

Like database sorting, XSL/XSLT can also implement random sorting, and the principle is also very simple. The following is the code.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http:///1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:eMeng="/"
  version="1.0">
<msxsl:script language="JavaScript" implements-prefix="eMeng">
 function Random() {
  return ();
 }
</msxsl:script>

<xsl:template match="/">
<xsl:for-each select="/*/node()">
<xsl:sort select="eMeng:Random()"/>
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>