繁体   English   中英

XQuery 从 XML 文件中删除属性为 onlyChannels="print" 的所有元素

[英]XQuery to remove all elements with attribute onlyChannels="print" from XML file

尝试从 XML 中删除所有具有属性onlyChannels="print"的元素 使用 XQuery,具有onlyChannels="print"的元素可以在任何地方和不同的级别。

输入 XML

<?xml version="1.0" encoding="UTF-8"?>
<abstractGroup>
   <abstract type="main" xml:lang="en">
      <title type="main">Abstract</title>
      <p>900 000 ha along the test of north</p>
      <p onlyChannels="print">Abstract</p>
   </abstract>
   <abstract onlyChannels="online" type="main" xml:lang="es">
      <title type="main">Resumen</title>
      <p>La orsdft de los trópifdaa</p>
   </abstract>
   <full type="main" xml:lang="en">
      <p onlyChannels="print">full</p>
      <p>900 000 ha along the test of north‐east</p>
      <doc>
      <p onlyChannels="print"> do not print</p>
      <p> print </p>
      </doc>
   </full>
</abstractGroup>

预计 output XML

<abstractGroup>
   <abstract type="main" xml:lang="en">
      <title type="main">Abstract</title>
      <p>900 000 ha along the test of north</p>
   </abstract>
   <abstract onlyChannels="online" type="main" xml:lang="es">
      <title type="main">Resumen</title>
      <p>La orsdft de los trópifdaa</p>
   </abstract>
   <full type="main" xml:lang="en">
      <p>900 000 ha along the test of north‐east</p>
     <doc>
      <p> print </p>
      </doc>
   </full>
</abstractGroup>

我正在尝试这个 XQuery,但它只删除了第一级中的元素并且没有 XML 标签。

let $root:=  abstractGroup/*/*[not(self::*/@onlyChannels="print")]
return $root

我得到了什么:

 Abstract 900 000 ha along the test of north Resumen La orsdft de los trópifdaa 900 000 ha along the test of north‐east do not print print

我如何打印 xml 标签并删除所有具有属性 onlyChannels="print" 的元素

您可以通过递归类型开关 function运行它来转换 XML:

declare function local:filter($nodes as node()*) as node()*
{
  for $n in $nodes return
  typeswitch ($n)
    case element () return 
      if ($n[@onlyChannels="print"]) 
      then local:filter($n/node()) 
      else element { node-name($n) } { $n/@*, local:filter($n/node())}
    default return $n
};

let $doc :=
<abstractGroup>
   <abstract type="main" xml:lang="en">
      <title type="main">Abstract</title>
      <p>900 000 ha along the test of north</p>
      <p onlyChannels="print">Abstract</p>
   </abstract>
   <abstract onlyChannels="online" type="main" xml:lang="es">
      <title type="main">Resumen</title>
      <p>La orsdft de los trópifdaa</p>
   </abstract>
   <full type="main" xml:lang="en">
      <p onlyChannels="print">full</p>
      <p>900 000 ha along the test of north‐east</p>
      <doc>
      <p onlyChannels="print"> do not print</p>
      <p> print </p>
      </doc>
   </full>
</abstractGroup>

return local:filter($doc)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM