【发布时间】: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 中,您可以将身份转换声明为基本模板,例如
<xsl:mode on-no-match="shallow-copy"/>并添加一个空模板<xsl:template match="*[@onlyChannels = 'print']"/>以删除任何此类元素,您就完成了。 -
在 XQuery Update 中,例如 BaseX 支持的,你只需要
delete nodes //*[@onlyChannels = 'print']。
标签: xml xquery xquery-3.0 xquery-3.1