【问题标题】:select query to remove nodes from xml column选择查询以从 xml 列中删除节点
【发布时间】:2019-03-25 16:32:43
【问题描述】:

我有一个带有 XML 列的表,其中包含 2 个具有大 base64 字符串(图像)的节点。当我查询数据库时,我想从返回给客户端的 xml 中删除这两个节点。我无法更改表的架构(即我无法拆分列中的数据)。如何使用 select 语句从 xml 列中删除 2 个节点? (要删除的节点都在其名称中包含文本“Image”)。在任何单个查询中最多可以返回 1000 条记录。

目前,我的查询基本上是这样的:

select top 1000 [MyXmlData] from [MyTable]

MyXmlData 列包含如下所示的 xml:

<MyXml>
   <LotsOfNodes></LotsOfNodes>
   ...
   <ANode>
      ...
      <MyImage1></MyImage1>   <!-- remove this from returned xml -->
      <MyImage2></MyImage2>   <!-- remove this from returned xml -->
      ...
   </ANode>
   ...
   <LotsOfNodes></LotsOfNodes>
   ...
</MyXml>

我使用的是 SQL Server 2008。

【问题讨论】:

    标签: sql xpath


    【解决方案1】:

    这是在 SQL Server 上测试的

    您可以将查询结果存储到临时表或表变量中,并使用modify()deleteImage 节点。使用contains()local-name() 确定是否应该删除节点。

    declare @T table(XmlData xml)
    
    insert into @T values
    ('<MyXml>
        <LotsOfNodes></LotsOfNodes>
        <ANode>
          <MyImage1></MyImage1>   <!-- remove this from returned xml -->
          <MyImage2></MyImage2>   <!-- remove this from returned xml -->
        </ANode>
        <LotsOfNodes></LotsOfNodes>
      </MyXml>')
    
    update @T set
      XmlData.modify('delete //*[contains(local-name(.), "Image")]')
    
    select *
    from @T
    

    结果:

    <MyXml>
      <LotsOfNodes />
      <ANode>
        <!-- remove this from returned xml -->
        <!-- remove this from returned xml -->
      </ANode>
      <LotsOfNodes />
    </MyXml>
    

    【讨论】:

      猜你喜欢
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2014-09-13
      相关资源
      最近更新 更多