我正在研究一个函数来合并一组序列,以尽可能地保留所有序列的顺序。在所有序列上执行不同值($序列)不会保留顺序。我需要一些保留顺序的XQuery序列合并的帮助
我有以下MarkLogic XQuery代码:
xquery version "1.0-ml";
declare function local:map-sequence($map, $list as xs:string*) {
let $count := fn:count($list) - 1
return for $idx in (1 to $count)
return if (map:contains($map, $list[$idx]))
then map:put($map, $list[$idx], fn:distinct-values((map:get($map, $list[$idx]), $list[$idx + 1])))
else map:put($map, $list[$idx], $list[$idx + 1])
};
declare function local:first($map) {
let $all-children := for $key in map:keys($map) return map:get($map, $key)
return distinct-values(map:keys($map)[not(.=$all-children)])
};
declare function local:next($map, $key as xs:string) {
if (map:contains($map, $key))
then if (fn:count(map:get($map, $key)) eq 1)
then map:get($map, $key)
else
let $children := map:get($map, $key)
return
for $next in $children
let $others := $children[fn:not(.=$next)]
let $descedents := local:descendents($map, $next)
return if ($descedents[.=$others])
then $next
else()
else()
};
declare function local:descendents($map, $key as xs:string) {
for $child in map:get($map, $key)
return ($child, local:descendents($map, $child))
};
declare function local:sequence($map, $key as xs:string) {
let $next := local:next($map, $key)
return if (fn:count($next) gt 1)
then
for $choice in $next
return $choice
else if (fn:count($next) eq 1)
then ($next, local:sequence($map, $next))
else()
};
let $map := map:map()
let $seq1 := local:map-sequence($map, ('fred', 'barney', 'pebbles'))
let $seq2 := local:map-sequence($map, ('fred', 'wilma', 'betty', 'pebbles'))
let $seq3 := local:map-sequence($map, ('barney', 'wilma', 'betty'))
let $first := local:first($map)
return ($map,
for $top in $first
return ($top, local:sequence($map, $top))
)
返回
{"barney":["pebbles", "wilma"], "fred":["barney", "wilma"], "wilma":"betty", "betty":"pebbles"}
fred
barney
wilma
betty
pebbles
它仍然需要工作。如果您添加:
let $seq4 := local:map-sequence($map, ('fred', 'bambam'))
bambam不显示。我仍在努力,但如果其他人有意见,那么我想听听他们。
感谢, 罗兰
为了您SEQ1 .. SEQ4请提供所需的最终输出。您要求保留序列,但您的示例输出会破坏重复项。因此我觉得很难理解,如果你真的想要所有物品返回或返回的顺序 - 但跳过任何以前提供的项目... –
要么fred,bambam,巴尼,威尔玛,贝蒂,鹅卵石或弗雷德,巴尼,bambam,威尔玛, betty,pebbles –
我正在使用XSD生成器,其中会有一个xs:barney和bambam选项作为选项。 –