XSLT 中的随机项
我正在自定义一个 Google Search Appliance,它使用 XSLT 向用户呈现结果。我们的设计要求在结果页面上随机包含几张图像之一。有没有办法在 XSLT 中使用随机性? (伪随机性对于这个应用程序来说就很好。)
调用随机模板就可以了,就像能够生成一个随机数并基于它的分支一样。
I'm customizing a Google Search appliance, which uses XSLT to present results to the user. Our design calls for one of several images to be included randomly on the results page. Is there a way to use randomness in XSLT? (Pseudo-randomness is just fine for this application.)
Calling random templates would be fine, as would just being able to generate a random number and branch based on that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下简单的解决方案极大地帮助了我使用
XSLT 2
(生成随机数):或者您可以将其用作函数:
以下是它的工作原理:
generate-id()
函数生成一个包含生成的 id 的字符串,例如wfx2d123d8
。string-to-codepoints()
函数将上一步获得的字符串转换为字符串 的 ASCII 码的节点列表,类似于以下内容:(119, 102, 120 、50、100、49、50、51、100、56)
。正如您所观察到的,提到的随机数生成器并不可靠并且缺乏均匀分布。因此,我强烈建议不要将其用于重要任务。但是,如果您需要一个简单的解决方案并且不关心分发(就像我的情况一样),那么它可能会作为一种选择。
The following simple solution greatly assisted me with
XSLT 2
(generates random number):Or you can use it as a function:
Here's how it works:
generate-id()
function generates a string containing the generated id, such aswfx2d123d8
.string-to-codepoints()
function transforms the string obtained in the previous step into a list of nodes of ASCII codes of the string , similar to the following:(119, 102, 120, 50, 100, 49, 50, 51, 100, 56)
.sum()
function calculates the sum of all the numbers obtained in step 2, resulting in a single valueAs you can observe, the random number generator mentioned is not reliable and lacks a uniform distribution. Therefore, I strongly advise against using it for important tasks. However, if you require a simple solution and are unconcerned about distribution, as was the case for me, it could potentially serve as an option.
您可以在纯 XSLT 序列中生成随机数以及 [1 .. N] 中的数字的随机排列。
只需使用 FXSL 库(用纯 XSLT 编写)用于此目的。
本文介绍了要使用的模板并包含完整的示例:
“使用 FXSL 掷骰子:XSLT 中的随机数生成函数”。
You can generate in pure XSLT sequences of random numbers and also random permutations of the numbers in [1 .. N].
Just use the FXSL library (written in pure XSLT) for this.
This article explains the templates to use and has complete examples:
"Casting the Dice with FXSL: Random Number Generation Functions in XSLT".
根据您的平台,XSL 允许像 C# 一样注入用户代码。我不推荐这个。更好的是,我会让您的 XSL 接受一个参数以及生成 XML 有效负载或 XSLT 的任何内容,并且还可以生成随机数,设置参数。我完全使用这种方法完成了此操作,只是数据来自 Bing,而不是 G。
Depending on your platform XSL allows inject of user code like C#. I don't recommend this. Better, I would have your XSL accept a parameter and whatever is generating your XML payload or XSLT and can also generate the random number, setting the parameter. I've done this exactly using this approach except the data came from Bing, not G.
如果您使用基于 Java 的 XSLT 引擎,这将允许您调用 Java 库中的任何静态方法,例如 java.lang.Math.random()。这是语法...
If you use a Java based XSLT engine, this will allow you to make calls to any static method within the Java libraries, such as java.lang.Math.random(). Here is the syntax...
如果您不反对包含库,则有许多可用的库,例如 random :EXSLT 中的随机序列
If you are not averse to including libraries, there are many available such as random:random-sequence from EXSLT
如果您正在为 Microsoft 做任何事情,我发现使用 XSLT 的函数 ddwrt:Random 是有效的。
我使用以下内容来创建随机数
并使用以下内容来呈现
If you are doing this for anything Microsoft, I found that using XSLT's function ddwrt:Random works.
I use the following to create the random number
and the following to present