簡體   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