xdmp:电子邮件过滤掉未传递谓词的电子邮件地址

时间:2016-02-02 20:32:48

标签: email xquery marklogic

假设我有一个谓语:

declare function local:should-send-email($email-address) {
  ... fn:true() ...
  ... fn:false() ...
};

然后我有一个用于xdmp:email的电子邮件元素:

<em:Message
 xmlns:em="URN:ietf:params:email-xml:"
 xmlns:rf="URN:ietf:params:rfc822:">
  <rf:subject>Test</rf:subject>
  <rf:from>
    <em:Address>
      <em:name>Test Name</em:name>
      <em:adrs>test-from@test000.com</em:adrs>
    </em:Address>
  </rf:from>
  <rf:to>
    <em:Address>
      <em:name>Test Name</em:name>
      <em:adrs>test-to-1@test000.com</em:adrs>
    </em:Address>
    <em:Address>
      <em:name>Test Name</em:name>
      <em:adrs>test-to-2@test000.com</em:adrs>
    </em:Address>
  </rf:to>
  <em:content>Hello World</em:content>
</em:Message>

让我们说其中一个to地址传递谓词而另一个不传递谓词。删除em的有效方法是什么:未通过的地址元素?

1 个答案:

答案 0 :(得分:3)

假设您打算从完整的<em:Message>元素开始,您可以使用递归类型切换来过滤掉无效的电子邮件地址 - 逻辑与XSLT非常相似:

declare function local:filter-addresses(
  $node as node()
) as item()*
{
  typeswitch ($node)
  case element(em:Address) return
    if (not(local:should-send-email($node/em:adrs)))
    then () (: Filter :)
    else $node
  case element() return (: Passthrough :)
    element { node-name($node) } {
      $node/@*,
      for $child in $node/node()
      return local:filter-addresses($child)
    }
  default return $node
};