xquery count for expressions in a for expressions

不想你离开。 提交于 2019-12-13 03:18:17

问题


question is how could i count the for expressions in a for expressions with unknow parent.

Code:

let $act :=(
<act time="1231">
     <data value="2">hello</data>
     <data value="2">hello</data>
     <data value="2">hello</data>
     <data value="2">hello</data>
     <data value="2">hello</data>
     <data value="2">hello</data>
</act>,
<act time="51292">
 <state>
     <data value="2">world</data>
     <data value="2">world</data>
     <data value="2">world</data>
     <data value="2">world</data>
     <data value="2">world</data>
 </state>
</act>,
<act time="41212">
 <state1>
    <state2>
     <data value="2">3</data>
     <data value="2">3</data>
     <data value="2">3</data>
     <data value="2">3</data>
     <data value="2">3</data>
    </state2>
 </state1>
</act>
)

for $data in $act
return
<act time ="{$data/@time}">
    {
    for $count at $cnt in $act//data
    where $count/.. is $data
    return
    <data value ="{$count}">{$cnt}</data>

    }
</act>

results:

<act time="1231">
   <data value="hello">1</data>
   <data value="hello">2</data>
</act>
<act time="51292"/>
<act time="41212"/>

expecting:

<act time="1231">
   <data value="hello">1</data>
   <data value="hello">2</data>
</act>,
<act time="51292">
   <data value="world">3</data>
   <data value="world">4</data>
</act>,
<act time="41212">
   <data value="3">5</data>
   <data value="3">6</data>
</act>

also link to the code what shows what is a result and what am i expecting https://xqueryfiddle.liberty-development.net/pPgCcoE/11


回答1:


I think you simply want to change the where clause to use where $count/ancestor::act is $data, i.e. to check whether the ancestor act element is the element bound to the $data variable of the outer for expression.




回答2:


In your code, when you iterate on element act then they treat as different fragment. So you should first merge all act in one markup as i use element mainact. Second issue is when you iterate on act element it always start counting form one for data element.

let $act :=(
<act time="1231">
    <data value="2">hello</data>
    <data value="2">hello</data>
    <data value="2">hello</data>
    <data value="2">hello</data>
    <data value="2">hello</data>
    <data value="2">hello</data>
</act>,
<act time="51292">
    <state>
        <data value="2">world</data>
        <data value="2">world</data>
        <data value="2">world</data>
        <data value="2">world</data>
        <data value="2">world</data>
    </state>
</act>,
<act time="41212">
    <state1>
        <state2>
            <data value="2">3</data>
            <data value="2">3</data>
            <data value="2">3</data>
            <data value="2">3</data>
            <data value="2">3</data>
        </state2>
    </state1>
</act>
)

let $markAct:=<mainact>{for $data in $act
    return 
    $data
    }</mainact>
let $data:=  

for $in in $markAct//act

return <act time="{$in/@time}">{
    for $data in $in//data
    return <data value="{$data}">{count($data/preceding::data)+1}</data>
    }</act>


return $data


来源:https://stackoverflow.com/questions/58397444/xquery-count-for-expressions-in-a-for-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!