【问题标题】:How to remove xml nodes without attribute in Sql Server如何在 Sql Server 中删除没有属性的 xml 节点
【发布时间】:2013-01-29 09:23:46
【问题描述】:

从 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>

【问题讨论】:

  • 您是想在表格中永久删除它们,还是在SELECT 期间隐藏它们?
  • 我想完全删除它们,损坏的数据只会导致开销。

标签: sql sql-server sql-server-2008 tsql sql-delete


【解决方案1】:

试试这个:

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

SQL FIDDLE DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多