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

2024-01-04

尝试删除所有具有属性的元素onlyChannels="print"使用 XQuery 从 XML 元素与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>

预期输出 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" 的所有元素


你可以通过一个运行它递归类型切换函数 https://en.wikibooks.org/wiki/XQuery/Typeswitch_Transformations转换 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐