XSLT 中的随机项

发布于 2024-08-23 13:47:21 字数 162 浏览 2 评论 0原文

我正在自定义一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

酒几许 2024-08-30 13:47:22

以下简单的解决方案极大地帮助了我使用 XSLT 2(生成随机数):

sum(string-to-codepoints(generate-id($generated//random))

或者您可以将其用作函数:

<xsl:function name=“your:random-int" as="xs:integer">
  <xsl:variable name="generated">
    <random/>
  </xsl:variable>
  <xsl:value-of select="sum(string-to-codepoints(generate-id($generated//random)))"/>
</xsl:function>

以下是它的工作原理:

  1. generate-id()函数生成一个包含生成的 id 的字符串,例如 wfx2d123d8
  2. string-to-codepoints() 函数将上一步获得的字符串转换为字符串 的 ASCII 码的节点列表,类似于以下内容:(119, 102, 120 、50、100、49、50、51、100、56)
  3. 最后,sum() 函数计算步骤 2 中获得的所有数字的总和,从而得出单个值

正如您所观察到的,提到的随机数生成器并不可靠并且缺乏均匀分布。因此,我强烈建议不要将其用于重要任务。但是,如果您需要一个简单的解决方案并且不关心分发(就像我的情况一样),那么它可能会作为一种选择。

The following simple solution greatly assisted me with XSLT 2 (generates random number):

sum(string-to-codepoints(generate-id($generated//random))

Or you can use it as a function:

<xsl:function name=“your:random-int" as="xs:integer">
  <xsl:variable name="generated">
    <random/>
  </xsl:variable>
  <xsl:value-of select="sum(string-to-codepoints(generate-id($generated//random)))"/>
</xsl:function>

Here's how it works:

  1. The generate-id() function generates a string containing the generated id, such as wfx2d123d8.
  2. The 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).
  3. Finally, the sum() function calculates the sum of all the numbers obtained in step 2, resulting in a single value

As 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.

原谅我要高飞 2024-08-30 13:47:21

您可以在纯 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".

冷夜 2024-08-30 13:47:21

根据您的平台,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.

我的痛♀有谁懂 2024-08-30 13:47:21

如果您使用基于 Java 的 XSLT 引擎,这将允许您调用 Java 库中的任何静态方法,例如 java.lang.Math.random()。这是语法...

<?xml version='1.0'?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:math="java.lang.Math"
    version='1.1'>

    <xsl:template match="/">
        <xsl:variable name="myRandom" select="math:random()"/>
        <xsl:value-of select="$myRandom"/>
    </xsl:template>

</xsl:stylesheet>

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...

<?xml version='1.0'?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:math="java.lang.Math"
    version='1.1'>

    <xsl:template match="/">
        <xsl:variable name="myRandom" select="math:random()"/>
        <xsl:value-of select="$myRandom"/>
    </xsl:template>

</xsl:stylesheet>
旧城空念 2024-08-30 13:47:21

如果您不反对包含库,则有许多可用的库,例如 random :EXSLT 中的随机序列

If you are not averse to including libraries, there are many available such as random:random-sequence from EXSLT

自此以后,行同陌路 2024-08-30 13:47:21

如果您正在为 Microsoft 做任何事情,我发现使用 XSLT 的函数 ddwrt:Random 是有效的。

我使用以下内容来创建随机数

<xsl:variable name="RowCount" select="count($Rows)" />
<xsl:variable name="RandomNumber" select="ddwrt:Random(1, $RowCount)" />

并使用以下内容来呈现

<xsl:for-each select="$Rows[position() = $RandomNumber]">
<xsl:value-of select="@Title" /></xsl:for-each>

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

<xsl:variable name="RowCount" select="count($Rows)" />
<xsl:variable name="RandomNumber" select="ddwrt:Random(1, $RowCount)" />

and the following to present

<xsl:for-each select="$Rows[position() = $RandomNumber]">
<xsl:value-of select="@Title" /></xsl:for-each>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文