【问题标题】:xquery matches - allow non existing nodes in loopxquery 匹配 - 允许循环中不存在的节点
【发布时间】:2019-11-10 17:07:43
【问题描述】:

我有一个 for 循环,想过滤一些节点,效果很好:

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

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

【问题讨论】:

    标签: xquery


    【解决方案1】:

    您可以使用fn:exists() 函数测试abcqwert 元素是否存在。如果您希望它在其中任何一个元素不存在的情况下通过,您可以使用fn:not() 来否定abcqwert 存在的测试:

    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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2018-04-27
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多