- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Xquery 3.1(在 eXist 4.7 中)中,我有 40 个 XML 文件,我需要随机选择其中 4 个。不过我希望这四个文件是不同的。
我的文件都在同一个集合中 ($data
)。我目前对文件进行计数,然后使用随机函数 ( util:random($max as xs:integer) ) 在文件序列中生成 position()
以选择其中四个:
let $filecount := count($data)
for $cnt in 1 to 4
let $pos := util:random($filecount)
return $data[position()=$pos]
但这通常会导致相同的文件被偶然选择多次。
每个文件都有一个不同的@xml:id
(在每个文件的根节点中),如果可能的话,它可以允许我将其用作递归中的某种谓词。但我无法确定以某种方式将 @xml:id
累积到累积递归序列中的方法。
感谢您的帮助。
最佳答案
我认为标准化的random-numer-generator
函数及其permute
函数( https://www.w3.org/TR/xpath-functions/#func-random-number-generator )应该为您提供更好的“随机性”和多样化的结果,例如
let $file-count := count($data)
return $data[position() = random-number-generator(current-dateTime())?permute(1 to $file-count)[position() le 4]]
我还没有尝试过使用您的 db/XQuery 实现,并且您当前使用的函数可能还有其他方法。
对于 eXist-db 我想一种策略是调用random-number
函数,直到你得到所需数量值的不同序列,以下返回(至少在一些测试中) eXide)) 每次调用时有四个 1 到 40 之间的不同数字:
declare function local:random-sequence($max as xs:integer, $length as xs:integer) as xs:integer+ {
local:random-sequence((), $max, $length)
};
declare function local:random-sequence($seq as xs:integer*, $max as xs:integer, $length as xs:integer) as xs:integer+ {
if (count($seq) = $length and $seq = distinct-values($seq))
then $seq
else local:random-sequence((distinct-values($seq), util:random($max)), $max, $length)
};
let $file-count := 40
return local:random-sequence($file-count, 4)
在之前的尝试中整合它会导致
let $file-count := count($data)
return $data[position() = local:random-sequence($file-count, 4)]
至于您的评论,我没有注意到现有的 util:random
函数可以返回 0 并排除最大值,因此根据您的评论和进一步的测试,我猜您更想要该函数我在上面发布了要实现的内容
declare function local:random-sequence($seq as xs:integer*, $max as xs:integer, $length as xs:integer) as xs:integer+ {
if (count($seq) = $length)
then $seq
else
let $new-number := util:random($max + 1)
return if ($seq = $new-number or $new-number = 0)
then local:random-sequence($seq, $max, $length)
else local:random-sequence(($seq, $new-number), $max, $length)
};
这样一来,它现在有望返回 1
和 $max
参数之间的 $length
不同值。
关于xpath - xquery随机选择文件而不重复选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58512986/
编辑:为什么这被否决了?我真的不知道...顺便说一句 ../不起作用,因为我不想要 Table 的父级但实际上想要 ../td+1 我不知道这是否可能? 嗨,大家好。 我手头有一个相当复杂的问题..
我很难找到需要单击的输入(复选框)元素的 xpath。我正在尝试使用其他跨度元素来定位它。元素包含 Angular 属性,不知道这是否重要? 元素的结构如下: Company name
我正在尝试构建一个包含许多 XPath 作为参数的 DSL。我是 XPath 的新手,我需要一个 XPath 语法中从未使用过的字符,这样我就可以在脚本的一行中分隔 n 个 XPath。我的问题:哪些
使用xpath在父标签内找到特定标签: 输入样例:
我需要构造一个通用XPath来找到正确的节点,其中的标准是日期和时间。例如查找“ 5 May”,“ 12:17:44”的节点 XML具有日期和时间标签。 不方便地,日期标签仅在当天的第一次出现时填充。
我正在尝试获取xPath几个月内两个日期之间的差异。 几天之内我就没问题了(1127) days-from-duration(xs:date('2012-06-17')-xs:date('2009-0
我试图选择一个包含一段文本的元素,但是我不想选择包含该文本加上其他文本的元素。我通常会使用text()='abc def',但是这个特定元素在前后都包含空格。 这是一个示例片段:
亲爱的,您能帮我用这个XPATH吗?可以说我有以下HTML代码 text value1 value2 text 我需要构建一
我正在尝试提取带有排除项的 xpath,但无法执行此操作。 (//div[@class='row site country-names']/following-sibling::div)[1]/di
response.xpath('//*[@id="blah"]//text()') 假设我的html是 This is a simple text foo and this is after tag.
除了那些具有"//ul/li[not(@*)][count(*)=0]"父项的人以外,我需要全部接受。我已经尝试过,但是不幸的是它不起作用。 有谁知道,我该怎么处理? 提前致谢。 最佳答案 我认为您需
我使用XPath的问题是,每当我使用“子字符串”功能时,我只会得到一个匹配项,而我想全部获得它们。 另一个问题是,每当我使用“子字符串”和运算符的组合时它只是行不通(没有匹配项)。 例如:http:/
我正在尝试通过其位置和属性获取项目,但不知道如何做。 我要实现的是将这一点统一起来: Xpath("//h4/a[contains(@href,'#vuln_')]") 还有这个: Xpath
我有一个xpath如下: .//*[text()='Name:']/../child::select | .//*[text()='Name:']/../child::span 但是对我来说,它既不紧
我拼命试图在xpath中组合几个过滤器。假设我的数据如下所示: DELETE 1 This is my title my sh
我想在已经通过 xpath 设置的其他元素中使用 xpath 来指示元素的位置。 下面的一个已经通过 xpath 设置(我没有改变) //Base_Code
是否可以使用xpath直接在括号内抓取信息?还是以后再用正则表达式过滤? HTML如下所示: Product name (UN1QU3 C0D3) 使用以下Xpath表达式,我可以在此中获取所有内容:
我试图使用一个XPath表达式来选择一个节点,该节点的子节点与文档中的另一个节点匹配。 匹配将意味着该节点的所有属性都相同。因此,如果将一个节点与多个属性进行比较,则无法进行单独的属性比较。 作为示例
我想在 XPath 表达式中使用 Iverson 括号(即映射 true => 1,false => 0)。 示例:而不是书写 someNumber+(if(elem1/elem2[@attr='12
是否可以以类似方式选择节点? './tr[position() in (1, 3, 7)]' 我只找到以下解决方案: './tr[position() = 1 or position() = 3 or
我是一名优秀的程序员,十分优秀!