xquery匹配-允许循环中不存在的节点

xquery匹配-允许循环中不存在的节点,xquery,Xquery,我有一个for循环,希望过滤一些节点,这很好: matches($doc/abc/@def, $filterA) matches($doc/qwert/@xyz, $filterB) 当$filterA、$filterB或两者都为空时,也可以返回每个节点。但是,如果节点abc或qwert不存在,则返回节点是无效的。对于我当前使用的默认值(”(空字符串),是否有其他默认值或其他函数可用于使其工作?您可以测试函数中是否存在abc和qwert元素。如果您希望在这些元素中的任何一个不存在的情况下通过测

我有一个for循环,希望过滤一些节点,这很好:

matches($doc/abc/@def, $filterA)
matches($doc/qwert/@xyz, $filterB)

$filterA
$filterB
或两者都为空时,也可以返回每个节点。但是,如果节点
abc
qwert
不存在,则返回节点是无效的。对于我当前使用的默认值(”(空字符串),是否有其他默认值或其他函数可用于使其工作?

您可以测试函数中是否存在
abc
qwert
元素。如果您希望在这些元素中的任何一个不存在的情况下通过测试,您可以使用来否定
abc
qwert
存在的测试:

fn:not(fn:exists($doc/abc) and fn:exists($doc/qwert))
如果要在
$filterA
$filterB
为空时通过条件:

fn:not(fn:exists($filterA) and fn:exists($filterB)) 
您可以将
matches()
表达式合并为谓词,以避免重复
$doc
(这不是一个巨大的节约,但在编写XPath表达式时需要考虑一些更一般的问题)

$doc[matches(abc/@def, $filterA) and matches(qwert/@xyz, $filterB)] 
总而言之:

let $filterA := "a"
let $filterB :="b"
let $doc := <doc><abc def="a"/><qwert xyz="b"/></doc>
return
  if (fn:not(fn:exists($doc/abc) and fn:exists($doc/qwert))
      or fn:not(fn:exists($filterA) and fn:exists($filterB)) 
      or $doc[matches(abc/@def, $filterA) and matches(qwert/@xyz, $filterB)])
  then "pass - copy nodes"
  else "fail"
let$filterA:=“a”
让$filterB:=“b”
让$doc:=
返回
if(fn:not(fn:exists($doc/abc)和fn:exists($doc/qwert))
或者fn:not(fn:exists($filterA)和fn:exists($filterB))
或$doc[匹配项(abc/@def,$filterA)和匹配项(qwert/@xyz,$filterB)])
然后“传递-复制节点”
否则“失败”