简体   繁体   English

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

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

The following output isn't too far off, but the query's creating a new record element for each entry record whereas the original data has four entry element nodes in each record.下面的 output 并不算太远,但是查询为每个entry记录创建一个新的record元素,而原始数据在每个记录中有四个entry元素节点。

In this way, some data is being erased.这样,一些数据正在被擦除。 There should be four entry nodes which are children of a record node, the record ;应该有四个entry节点,它们是record节点record的子节点; the root element is csv .根元素是csv

How can I add attributes to each entry element without altering the document tree?如何在不更改文档树的情况下向每个entry元素添加属性?

What I'm looking for is like:我正在寻找的是这样的:

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

so that the "counter" restarts for each record.以便“计数器”为每条记录重新启动。

current output:当前 output:

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

input:输入:

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

Looking to maintain the structure of the original document above.希望保持上面原始文档的结构。

You can nest all such structures eg您可以嵌套所有此类结构,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM