我想要一个解决方案,以获取如何在xQuery中将实际循环计数器保存为Java,而不是针对我的特定简化演示的解决方法。我知道在xQuery 3.0中有一个用于FLOWR结构的计数保留字,但它没有用的前撒克逊9.something。这是我的例子。
for $elem at $x in /Data/* return element Elem {
attribute Name {concat('_',$x,'.',name($elem))},
(: ...10 lines of code... :)
for $subelem at $y in $elem/*
(: ...5 lets... :)
return element SubElemen {
attribute Name {concat('_',$x,'.',$y,'_',name($subelement))},
(...20 lines of other attrs and elements ...)
for $subsubelem at $z in $subElem/SubSubElement
let $absIterIndex := 'THIS IS WHAT IM LOOKING'
let $subSubElemName := concat('_',$absIterIndex,'_',name($subElem))
return
element SubSubElem {
attribute Name {$subsubelem},
(..100 lines more playing with $x, $y, $absIterIndex, all lets for that iteration, attributes depending on them...)
输出是这样的(真实数据隐藏和简化)
<Elem Name="_1.Name">
<SubElem Name="_1.1_Name">
<SubSubElem Name="_1.First"/>
<SubSubElem Name="_2.Second"/>
<SubSubElem Name="_3.Third"/>
</SubElem Name="_1.1_Name">
<SubElem Name="_1.2_Name">
<SubSubElem Name="_4.Fourth"/>
<SubSubElem Name="_5.Fifth"/>
<SubSubElem Name="_6.Sixth"/>
<SubSubElem Name="_7.Seventh"/>
</SubElem Name="_1.1_Name">
<Elem Name="_2.Name">
<SubElem Name="_2.1_Name">
<SubSubElem Name="_8.Eighth"/>
<SubSubElem Name="_9.Ninth"/>
<SubSubElem Name="_10.Tenth"/>
<SubSubElem Name="_11.Eleventh"/>
</SubElem Name="_2.1_Name">
<SubElem Name="_2.2_Name"/>
</Elem Name="_2.Name">
<Elem Name="_3.Name">
<SubElem Name="_3.1_Name">
<SubSubElem Name="_12.Twelven"/>
</SubElem Name="_3.1_Name">
<SubElem Name="_3.2_Name"/>
</Elem Name="_3.Name">
这是前一个问题Autoincremental auxiliar index var for XQuery nested loops的扩展,我接受了xQuery 3.0计数器的解决方案,然后提出了手动执行此操作的替代方案。但我知道我认为这种观点是不同的,更具体,并且有一个新的例子。如果您认为应删除它,请告诉我。
答案 0 :(得分:1)
你可以做的是在嵌套迭代的结果序列上添加一个额外的传递,即
declare variable $script := 'abc defg h ijklm nop
qrs tu vw
xy z';
for $letter at $lettersTotal in
(
for $line at $numLine in tokenize($script, '(\r\n?|\n\r?)')
for $word at $numWord in tokenize(normalize-space($line),' ')
for $codepoint at $numLetter in string-to-codepoints($word)
return codepoints-to-string($codepoint)
)
return ($lettersTotal, $letter)
返回
1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 10 j 11 k 12 l 13 m 14 n 15 o 16 p 17 q 18 r 19 s 20 t 21 u 22 v 23 w 24 x 25 y 26 z
如果幸运的话,您的XQuery处理器可以在不实现序列的情况下传输结果。
答案 1 :(得分:0)
新问题 - 新答案。
但原则仍然相同:首先产生无编号的结果,然后应用编号。这可能需要重写结果。
插入代码而不是下面的XML注释。
declare function local:renumber($number, $string)
{
replace($string, 'THIS IS WHAT IM LOOKING', string($number))
};
declare function local:renumber($subSubElems, $number, $nodes)
{
for $node in $nodes
return
typeswitch ($node)
case attribute() return attribute {node-name($node)} {local:renumber($number, $node)}
case processing-instruction() return processing-instruction {name($node)} {local:renumber($number, $node)}
case comment() return comment {local:renumber($number, $node)}
case text() return text {local:renumber($number, $node)}
default (: element() :) return
element {node-name($node)}
{
local:renumber
(
$subSubElems,
if ($subSubElems[. is $node]) then count($subSubElems[. << $node]) + 1 else $number,
($node/@*, $node/node())
)
}
};
let $result := <result>{<!-- your code here -->}</result>
return local:renumber($result//SubSubElem, 0, $result/node())