2011-05-31 84 views
3

我有一個包含2個具有大型base64字符串(圖像)的節點的XML列的表。當我查詢數據庫時,我想從返回給客戶端的xml中移除這2個節點。我無法更改表格的架構(即我無法拆分列中的數據)。我如何使用select語句從xml列中刪除2個節點? (要刪除的節點都包含名稱中的文本「Image」)。可以在任何單個查詢中返回1000個記錄。選擇查詢以從xml列中刪除節點

目前,我的查詢基本上是這樣的:

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> 
+1

是否使用的是RDBMS? – 2011-05-31 13:07:32

+0

哎呀,應該包括我猜 - sql2008 – jimasp 2011-06-15 10:34:34

回答

3

這是SQL Server的測試

你可以將您的查詢結果存儲到臨時表或表變量中,並使用modify()delete,Image節點。使用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> 
+0

謝謝Mikael。就是這份工作。 – jimasp 2011-06-15 10:32:19