【问题标题】:XQuery to remove all elements with attribute onlyChannels="print" from XML fileXQuery 从 XML 文件中删除所有具有属性 onlyChannels="print" 的元素
【发布时间】:2021-10-04 12:04:45
【问题描述】:

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

【问题讨论】:

  • 那是哪个 XQuery 处理器?它是否支持 XQuery 更新?或者它是否也支持(如 Saxon 10 或 9)XSLT?在 XSLT 中,您可以将身份转换声明为基本模板,例如&lt;xsl:mode on-no-match="shallow-copy"/&gt; 并添加一个空模板 &lt;xsl:template match="*[@onlyChannels = 'print']"/&gt; 以删除任何此类元素,您就完成了。
  • 在 XQuery Update 中,例如 BaseX 支持的,你只需要delete nodes //*[@onlyChannels = 'print']

标签: xml xquery xquery-3.0 xquery-3.1


【解决方案1】:

您可以通过recursive typeswitch 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多