如何在文件夹中的所有XML文档上执行XQuery

问题描述 投票:1回答:1

我需要确保许多XML文件中存在特定节点。每次我想查询另一个文档时,我都必须切换上下文。

enter image description here

有没有办法在不切换上下文的情况下在目录中的所有文档上执行XQuery?

xquery basex
1个回答
1
投票

我可能有点晚了,但很可能下面的XQuery会按照你的意愿行事,它会返回不包含特定元素的每个XML文件的路径:

let $path := "."
for $file in file:list( $path, true(), '*.xml')
  let $path := $path || "/" || $file
  where not(
    exists(fetch:xml($path)/foo/bar[text() = "Text"])
  )  
return $path 

如果您只对特定的XML文件感兴趣,或者不包含特定元素,则以下查询可能有用:

declare variable $path := "/Users/michael/Code/foo";

every $doc in file:list($path, true(), '*.xml') (: returns a sequence of file-names :)
              => for-each(concat($path,"/", ?)) (: returns a sequence of full paths to each file :)
              => for-each(fetch:xml#1)          (: returns a sequence of documents :)
satisfies exists(
  $doc/*/*[text() = "Text"]
)

希望这可以帮助 ;-)

© www.soinside.com 2019 - 2024. All rights reserved.