0

I want a solution for udnerstanding how to hold a real loop counter as Java in xQuery, not a workaround for my particular simplified demo. I know in xQuery 3.0 there is a count reserved word for FLOWR structures but its useless pre-Saxon 9.something. Here my example.

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

The output is something like this (real data hidden and simplified)

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

This is a extension of a previous question Autoincremental auxiliar index var for XQuery nested loops, which I accepted the solution for the xQuery 3.0 counter, and then propose an alternative for manually doing it. But know i think the perspective is different and more concrete, and with a new example. If you think it should be deleted just tell it to me.

4

2 回答 2

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 处理器可以流式传输结果而无需具体化序列。

于 2012-07-03T21:27:51.063 回答
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())
于 2012-07-05T11:26:43.260 回答