简体   繁体   中英

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

Trying to remove all elements with attribute onlyChannels="print" from XML Using XQuery the element with onlyChannels="print" can be anywhere and at different levels.

Input 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>

expcted 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>

I was trying this XQuery but it's only removing elements in the first level and without XML tags.

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

what I got:

 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

How I can print the xml tags and removing all elements with attribute onlyChannels="print"

You could run it through arecursive typeswitch function to transform the 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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