Autoincremental auxiliar index var for XQuery nest

2019-08-22 01:08发布

问题:

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.

回答1:

New question - new answer.

But the principle remains the same: produce an unnumbered result first, then apply the numbering. This may require rewriting the result.

Insert your code instead of the XML comment below.

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


回答2:

What you could do is add an extra pass over the result sequence of your nested iterations, i.e.

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)

This returns

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

If you are lucky, your XQuery processor can stream the result without materializing the sequence.