XQuery中的循环多次重复结果



我有一个从文本返回段落的函数。所以我比较<anchor>标签的属性号(@n)到<notes>标签的属性号,如果它是相同的,我想用工具提示打印它,如果不是,我只是想打印出段落。

declare function letter:text_trans($node as node(), $model as map(*))
{
    let $resource := collection('/db/apps/Tobi-oshki/data')
    for $ab in $resource//tei:div[@type="translation"]/tei:ab
    for $note in $resource//tei:div[@type="notes"]/tei:note
    return
        if (data($note/@n) eq data($ab/tei:anchor/@n))
        then
          <div class="tooltip">{$ab}
            <span class="tooltiptext"> {data($note/@n)}.{$note}</span>
          </div>
        else
          <p> {$ab} </p>
    
};

<notes>中,我有三个音符,当它循环音符时,每个段落返回三次。

我如何改变它,使它只返回段落一次?

我使用xquery version "3.1";

$ab的for循环中,为$note设置一个变量,并选择具有与$ab匹配的@n属性值的注释,然后如果有匹配的$note,使用它,否则仅使用$ab返回<p>:

let $resource := collection('/db/apps/Tobi-oshki/data')
for $ab in $resource//tei:div[@type="translation"]/tei:ab
let $note := $resource//tei:div[@type="notes"]/tei:note[@n = $ab/tei:anchor/@n]
return
    if ($note)
    then
      <div class="tooltip">{$ab}
        <span class="tooltiptext"> {data($note/@n)}.{$note}</span>
      </div>
    else
      <p> {$ab} </p>  

输入:

<tei:doc>
  <tei:div type="notes">
    <tei:note n="1">note1</tei:note>
    <tei:note n="2">note2</tei:note>
  </tei:div>
  <tei:div type="translation">
    <tei:ab><tei:anchor n="1">translated note1</tei:anchor></tei:ab>
    <tei:ab><tei:anchor n="3">translated note3</tei:anchor></tei:ab>
  </tei:div>
</tei:doc>
上面的代码产生如下输出:
<div class="tooltip">
  <tei:ab xmlns:tei="tei">
    <tei:anchor n="1">translated note1</tei:anchor>
  </tei:ab>
  <span class="tooltiptext">1.<tei:note n="1" xmlns:tei="tei">note1</tei:note>
  </span>
</div>
<p>
  <tei:ab xmlns:tei="tei"><tei:anchor n="3">translated note3</tei:anchor></tei:ab>
</p>

最新更新