XQuery 在 MarkLogic 中获取多值字段

XQuery to get a multi-valued field in MarkLogic

我收到此错误,因为我们将字段设置为多值(多个相同元素的列表):

argX is not of type xs:anyAtomicType?

这是我们用来从 MarkLogic 获取值的查询:

declare variable $uris as xs:string  external;
for $uri in tokenize($uris,';')
    let $doc := fn:doc($uri)
    return xdmp:gzip(
        xdmp:unquote(fn:concat(
            "<item>",
            "<uri>",
            $uri,
            "</uri>",
            "<date-loaded>",
            $doc/date-loaded,
            "</date-loaded>",
            "<collections>",
                string-join(xdmp:document-get-collections($uri), ";"),
            "</collections>",
            "</item>"
        ))
    )

现在字段 <date-loaded> 可以有多个值,如下所示:

<date-loaded>2020-01-01</date-loaded>
<date-loaded>2020-01-02</date-loaded>
<date-loaded>2020-01-03</date-loaded>

这里的顺序很重要。我应该如何以正确的方式更改此查询以检索 date-loaded 的所有值并将它们分别放在单独的 XML 元素中?

如果您不创建要解析为 XML 的字符串,而是利用 XQuery 语言为您做这件事,事情会更容易和更简单:

for $uri in tokenize($uris,';')
let $doc := fn:doc($uri)
return 
  xdmp:gzip(<item>
              <uri>{$uri}</uri>
              {$doc/date-loaded} 
              <collections>{string-join(xdmp:document-get-collections($uri), ";") }</collections>
            </item>)

如果您希望这些 date-loaded 元素以特定顺序列出,而不是源中的 document-order,您可以按 FLOWR 对它们进行排序:

{ 
  for $date in $doc/date-loaded
  order by $date
  return $date
}