如何使用 FLWOR 添加基于嵌套索引计数器的节点属性?

How to add a node attribute based on a nested index counter using FLWOR?

以下输出相差不大,但是查询为每个 entry 记录创建了一个新的 record 元素,而原始数据在每个记录中有四个 entry 元素节点记录。

这样,一些数据被删除了。应该有四个 entry 节点,它们是 record 节点的子节点,即 record;根元素是 csv.

如何向文档树中的每个 entry 元素 without altering 添加属性?

我要找的是这样的:

<record>
<entry num="1">2020-01-26</entry>
<entry num="2">Vancouver Coastal</entry>
..
<record>
<entry num="1">2020-02-02</entry>
..

以便 "counter" 为每个记录重新启动。

当前输出:

<csv>
  <record>
    <entry num="1">2020-01-26</entry>
  </record>
  <record>
    <entry num="2">Vancouver Coastal</entry>
  </record>
  <record>
    <entry num="3">M</entry>
  </record>
  <record>
    <entry num="4">40-49</entry>
  </record>
  <record>
    <entry num="5">Lab-diagnosed</entry>
  </record>
  <record>
    <entry num="6">2020-02-02</entry>
  </record>
  <record>
    <entry num="7">Vancouver Coastal</entry>
  </record>
  <record>
    <entry num="8">baz</entry>
  </record>
  <record>
    <entry num="9">50-59</entry>
  </record>
  <record>
    <entry num="10">Lab-diagnosed</entry>
  </record>
  <record>
    <entry num="11">2020-02-05</entry>
  </record>
  <record>
    <entry num="12">Vancouver Coastal</entry>
  </record>
  <record>
    <entry num="13">F</entry>
  </record>
  <record>
    <entry num="14">20-29</entry>
  </record>
  <record>
    <entry num="15">Lab-diagnosed</entry>
  </record>
</csv>

xquery:

<csv>
{    

for $x at $i in db:open("bccdc_covid19.abbreviated")/csv/record/entry

return <record><entry num="{$i}">{$x/@*, $x/node()}</entry></record>

}


</csv>

输入:

<csv>
  <record>
    <entry>2020-01-26</entry>
    <entry>Vancouver Coastal</entry>
    <entry>M</entry>
    <entry>40-49</entry>
    <entry>Lab-diagnosed</entry>
  </record>
  <record>
    <entry>2020-02-02</entry>
    <entry>Vancouver Coastal</entry>
    <entry>baz</entry>
    <entry>50-59</entry>
    <entry>Lab-diagnosed</entry>
  </record>
  <record>
    <entry>2020-02-05</entry>
    <entry>Vancouver Coastal</entry>
    <entry>F</entry>
    <entry>20-29</entry>
    <entry>Lab-diagnosed</entry>
  </record>
</csv>

希望保持上面原始文档的结构。

您可以嵌套所有此类结构,例如

<csv>
{
    for $record in csv/record
    return
        <record>
        {
            for $entry at $pos in $record/entry
            return
                <entry num="{$pos}">{data($entry)}</entry>
        }
        </record>
}
</csv>