gpt4 book ai didi

sql - 如何删除Sql Server中没有属性的xml节点

转载 作者:行者123 更新时间:2023-12-02 05:16:17 25 4
gpt4 key购买 nike

从 Sql Server 2008 的 xml 字段中删除具有缺失属性的根节点的所有子节点的正确方法是什么?

我的Xml是这样的,我要去掉<root>的所有子节点没有 ln指定的属性

<root>
<title />
<title />
<questionphrase ln="nl">
<xhtml />
</questionphrase>
<questionphrase ln="en">
<xhtml />
</questionphrase>
<information ln="nl">
<xhtml />
</information>
<information ln="en">
<xhtml />
</information>
<title />
<title ln="en">
value
</title>
<label ln="en">
value
</label>
<title />
<title />
</root>

删除后的xml应该是这样的

<root>
<questionphrase ln="nl">
<xhtml />
</questionphrase>
<questionphrase ln="en">
<xhtml />
</questionphrase>
<information ln="nl">
<xhtml />
</information>
<information ln="en">
<xhtml />
</information>
<title ln="en">
value
</title>
<label ln="en">
value
</label>
</root>

最佳答案

试试这个:

DECLARE @xml XML = '....'
SET @xml.modify('delete //root/*[not(@ln)]')

SQL FIDDLE DEMO

关于sql - 如何删除Sql Server中没有属性的xml节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14579462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com