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

时间:2019-01-18 09:48:55

标签: xquery basex

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

enter image description here

是否可以在不切换上下文的情况下对目录中的所有文档执行XQuery?

1 个答案:

答案 0 :(得分: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"]
)

希望这会有所帮助;-)