<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/><xsl:param name="initial-seed" select="123"/><xsl:template match="/"> <values> <xsl:call-template name="pick-random-item"> <xsl:with-param name="items" select="values/item"/> </xsl:call-template> </values></xsl:template><xsl:template name="pick-random-item"> <xsl:param name="items" /> <xsl:param name="seed" select="$initial-seed"/> <xsl:if test="$items"> <!-- generate a random number using the "linear congruential generator" algorithm --> <xsl:variable name="a" select="1664525"/> <xsl:variable name="c" select="1013904223"/> <xsl:variable name="m" select="4294967296"/> <xsl:variable name="random" select="($a * $seed + $c) mod $m"/> <!-- scale random to integer 1..n --> <xsl:variable name="i" select="floor($random div $m * count($items)) + 1"/> <!-- write out the corresponding item --> <xsl:copy-of select="$items[$i]"/> <!-- recursive call with the remaining items --> <xsl:call-template name="pick-random-item"> <xsl:with-param name="items" select="$items[position()!=$i]"/> <xsl:with-param name="seed" select="$random"/> </xsl:call-template> </xsl:if></xsl:template></xsl:stylesheet>应用于具有默认初始种子(123)的输入,输出为:Applied to your input with the default initial-seed (123), the output is:<?xml version="1.0" encoding="utf-8"?><values> <item>item 2</item> <item>item 3</item> <item>item 1</item> <item>item 4</item> <item>item 5</item></values>当种子为1234时执行,输出为:When performed with a seed of 1234, the output is:<?xml version="1.0" encoding="utf-8"?><values> <item>item 4</item> <item>item 1</item> <item>item 5</item> <item>item 2</item> <item>item 3</item></values> 这篇关于随机化节点顺序xslt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!