[英]XQuery Remove Elements - Attributes
I have an XML from end system which require to remove all empty elements or attributes that have empty or null
value. 我有一个来自最终系统的XML,它要求删除所有具有
empty or null
值empty or null
值的空元素或属性。
Example 例
<Element>
<fn att=""></fn>
<fn att="1"></fn>
<fn></fn>
<fn att="">
<child>1</child>
</fn>
</Element>
This is my script given using XQuery to check and remove them 这是我使用XQuery检查并删除它们的脚本
declare function local:remove-empty-elements($nodes as node()*) as node()* {
for $node in $nodes
return
if ($node instance of element())
then if (
(normalize-space($node) = '')
and (not(normalize-space($node/@*)))
)
then ()
else element { node-name($node)
}
{ $node/@*, local:remove-empty-elements($node/node())}
else if ($node instance of document-node())
then local:remove-empty-elements($node/node())
else $node
};
This is successfully remove the elements and tag node that are single. 这样可以成功删除单个元素和标签节点。 But when i put multiple attributes in one element
但是当我将多个属性放在一个元素中时
eg <fn att1="Data" att2="1"></fn>
例如
<fn att1="Data" att2="1"></fn>
The function itself fails. 函数本身失败。
Any ideas how to achieve this? 任何想法如何实现这一目标?
Thank you before. 谢谢你
** UPDATE ** **更新**
As per har07 answer, if i want to change the function to remove the attribute that having empty value, is there any suggestions? 按照har07的答案,如果我想更改功能以删除具有空值的属性,有什么建议吗? I try with the logic if below but still sending the same result.
我尝试使用下面的逻辑,但仍然发送相同的结果。
declare function local:remove-empty-elements($nodes as node()*) as node()* {
for $node in $nodes
return
if ($node instance of element())
then if (
(normalize-space($node) = '')
and (not($node/@*[normalize-space(.)]))
)
then (
/* updated code here */
if(($node/@*[normalize-space(.)])= '') then ()
else
$node/@*[normalize-space(.)]
)
else element { node-name($node)
}
{ $node/@*, local:remove-empty-elements($node/node())}
else if ($node instance of document-node())
then local:remove-empty-elements($node/node())
else $node
};
This part was causing the problem : 这是造成问题的原因:
and (not(normalize-space($node/@*)))
Call normalize-space()
function in predicate for individual attribute instead of passing all attributes to the function at once, like so : 为单个属性的谓词调用
normalize-space()
函数,而不是一次将所有属性传递给该函数,如下所示:
and (not($node/@*[normalize-space(.)]))
update : 更新:
Regarding the question update, you can use the same predicate to filter the attributes so that you end up with non-empty attributes only. 关于问题更新,您可以使用相同的谓词来过滤属性,以便最终只获得非空属性。 In the inner
else
block, set content of the returned element as follow : 在内部
else
块中,将返回元素的内容设置如下:
else element {node-name($node)}
{
$node/@*[normalize-space(.)],
local:remove-empty-elements($node/node())
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.