简体   繁体   English

在 xslt 1.0 中随机排序?

[英]Randomize sorting in xslt 1.0?

I would like to know, is there is any way to do random sorting in XSLT 1.0?我想知道,有没有办法在 XSLT 1.0 中进行随机排序?

Here is my XML这是我的 XML

<root><DO status="a">text comes here</DO><DO status="b">text comes here</DO><DO status="c">text comes here</DO><DO status="d">text comes here</DO><DO status="e">text comes here</DO></root>

Desired Output:所需的 Output:

<root><DO status="c">text</DO><DO status="a">text comes here</DO><DO status="b">text comes here</DO><DO status="e">text comes here</DO><DO status="d">text comes here</DO></root>

Hope my question is clear?希望我的问题很清楚?

Thanks in advance提前致谢

I would like to know, is there is any way to do random sorting in XSLT 1.0?我想知道,有没有办法在 XSLT 1.0 中进行随机排序?

With vanilla XSLT 1.0 - No.带香草 XSLT 1.0 - 编号

You could use an extension to access the randomizer of an external language and put that function into xsl:sort .您可以使用扩展来访问外部语言的随机化器,并将 function 放入xsl:sort中。 For example, using the msxsl extension to access Windows Scripting languages:例如,使用msxsl扩展访问 Windows 脚本语言:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:my="http://tempuri.org/myscripts" 
  exclude-result-prefixes="msxsl my"
>

  <msxsl:script language="JScript" implements-prefix="my">
    function random() {
      return Math.random();
    }
  </msxsl:script>

  <xsl:template match="root">
    <xsl:for-each select="DO">
      <xsl:sort select="my:random()" data-type="number" />
      <xsl:copy-of select="." />
    </xsl:for-each>
  </xsl:template> 

</xsl:stylesheet>

You can use XSLT generate-id() function which returns a string that uniquely identifies a node in the document.您可以使用XSLT generate-id() function返回一个唯一标识文档中节点的字符串。 In accordance with specification:按照规范:

An implementation is free to generate an identifier in any convenient way provided that it always generates the same identifier for the same node and that different identifiers are always generated from different nodes.一个实现可以以任何方便的方式自由地生成一个标识符,只要它总是为同一个节点生成相同的标识符,并且总是从不同的节点生成不同的标识符。 An implementation is under no obligation to generate the same identifiers each time a document is transformed.每次转换文档时,实现没有义务生成相同的标识符。 There is no guarantee that a generated unique identifier will be distinct from any unique IDs specified in the source document.无法保证生成的唯一标识符与源文档中指定的任何唯一 ID 不同。

So it depends on your XSLT processor.所以这取决于你的 XSLT 处理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM