for 循环作为谓词 xquery, xpath

for loop as predicate xquery, xpath

我有一个 /bibl 条目列表,每个条目都有一个 /date @when="YEAR" 子元素。我试图只输出每年的第一个实例。

`<listBibl>
            <bibl>
                <date when="1746"/> 
            </bibl>
            <bibl>
                <date when="1746"/> 
            </bibl>
            <bibl>
                <date when="1746"/> 
            </bibl>
            <bibl>
                <date when="1746"/>
            </bibl>
          <bibl>
                <date when="1747"/> 
            </bibl>
            <bibl>
                <date when="1747"/>
            </bibl>
            <bibl>
                <date when="1747"/> 
            </bibl>
          <bibl>
                <date when="1754"/>
            </bibl>
            <bibl>
                <date when="1754"/>
            </bibl>
          </listBibl>

我写了一个表达式来找到这个:for $d in /date/@when return (/date[@when=$d])[1] 但这在我用来输出属性值的程序(teipublisher)中不起作用。有人对我如何以不同的方式做这件事有什么建议吗?

通过使用 BaseX,v.9.6.4

XQuery

xquery version "3.1";

declare context item := document {
<listBibl>
    <bibl>
        <date when="1746"/>
    </bibl>
    <bibl>
        <date when="1746"/>
    </bibl>
    <bibl>
        <date when="1746"/>
    </bibl>
    <bibl>
        <date when="1746"/>
    </bibl>
    <bibl>
        <date when="1747"/>
    </bibl>
    <bibl>
        <date when="1747"/>
    </bibl>
    <bibl>
        <date when="1747"/>
    </bibl>
    <bibl>
        <date when="1754"/>
    </bibl>
    <bibl>
        <date when="1754"/>
    </bibl>
</listBibl>
};

<listBibl>
{
  for $x in ./listBibl/bibl
  let $date := $x/date/@when
  group by $date
  order by $date
  return <bibl>
        <date when="{$date}"/>
    </bibl>
}
</listBibl>

输出XML

<listBibl>
  <bibl>
    <date when="1746"/>
  </bibl>
  <bibl>
    <date when="1747"/>
  </bibl>
  <bibl>
    <date when="1754"/>
  </bibl>
</listBibl>