Xquery 从目录获取文档时按元素排序

Xquery 从目录获取文档时按元素排序,xquery,marklogic,Xquery,Marklogic,我必须从一个目录中获取所有文档,并使用下面的查询来执行相同的操作。现在我需要返回根据effectiveDate元素排序的结果。我们可以在代码中使用order by吗 let $news :=xdmp:directory("/news/","1") for $d in $news return $d -- Result ----- <?xml version="1.0" encoding="UTF-8"?> <NewsEntity xmlns="http://

我必须从一个目录中获取所有文档,并使用下面的查询来执行相同的操作。现在我需要返回根据effectiveDate元素排序的结果。我们可以在代码中使用order by吗

let $news :=xdmp:directory("/news/","1")
for $d in $news
return $d
-- Result -----

    <?xml  version="1.0" encoding="UTF-8"?>
    <NewsEntity xmlns="http://jnj.com/news">
        <uuid xmlns="">868e8a3a-058d-4b2d-8d69-0696f75ec97f</uuid>
        <headLine>HeadLine 4</headLine>
        <contributor>User 4</contributor>
        <effectiveDate>2016-08-31</effectiveDate>
    </NewsEntity>
    <?xml  version="1.0" encoding="UTF-8"?>
    <NewsEntity xmlns="http://jnj.com/news">
        <uuid xmlns="">311eeede-2560-4142-b882-b666ab08c9f8</uuid>
        <headLine>HeadLine 3</headLine>
        <contributor>User 3</contributor>
        <effectiveDate>2016-08-28</effectiveDate>
    </NewsEntity>
    <?xml  version="1.0" encoding="UTF-8"?>
    <NewsEntity xmlns="http://jnj.com/news">
        <uuid xmlns="">9bb67977-a217-425f-82e4-b4366e80d7c4</uuid>
        <headLine>HeadLine 2</headLine>
        <contributor>User 2</contributor>
        <effectiveDate>2016-08-30</effectiveDate>
    </NewsEntity>
let$news:=xdmp:directory(“/news/”,“1”)
在$news中的$d
返回$d
--结果-----
868e8a3a-058d-4b2d-8d69-0696f75ec97f
标题4
用户4
2016-08-31
311EEDE-2560-4142-b882-b666ab08c9f8
标题3
用户3
2016-08-28
9bb67977-a217-425f-82e4-b4366e80d7c4
标题2
用户2
2016-08-30

如果您希望对结果进行有效排序,则需要在
effectiveDate
上设置一个
date
范围索引。在某些条件下,查询优化器可以使用
orderby
子句利用该索引,但使用
cts:search
可能更直接。比如:

cts:search(collection(),
  cts:directory-query('/news', 1),
  cts:index-order(
    cts:element-reference(
      fn:QName("http://jnj.com/news", "effectiveDate")
      "type=date"
    )
  )
)
有关更多详细信息,请参阅


根据您的使用条件,您还可以使用更简单的“order by”表达式,这听起来像是您想要做的。在这种情况下,您必须确保正确地指向effectiveDate元素,因为它位于命名空间中

for $d in xdmp:directory("/news/","1")
order by $d/*:effectiveDate
return $d

如果文档集很大,这可能会导致性能非常差。这不是一个坏的/错误的方法,但是使用范围索引的cts:search仍然可以在大量文档上执行,而随着目录中文档数量的增加,搜索速度会显著降低。