0

I am trying to update a specific column in a table that has multiple namespaces in it. My table structure is:

+------+---------+-----------------------------------------------------------------------+
| cid  | cidtype |                               xml_data                                |
+------+---------+-----------------------------------------------------------------------+
| 1001 | N       | <Main  xmlns:json=""http://www.samplenamespace.com/json"" >           |
|      |         | <ID json:ValueType=""Number"">1001</ID>                               |
|      |         | <details>                                                             |
|      |         | <name xmlns:json=""http://www.samplenamespace.com/json"">John</name>  |
|      |         | <age  xmlns:json=""http://www.samplenamespace.com/json"">12</age>     |
|      |         | </details>                                                            |
|      |         | </Main>                                                               |
| 1003 | N       | <Main  xmlns:json=""http://www.samplenamespace.com/json"" >           |
|      |         | <ID json:ValueType=""Number"">1003</ID>                               |
|      |         | <details>                                                             |
|      |         | <name xmlns:json=""http://www.samplenamespace.com/json"">Diane</name> |
|      |         | <age  xmlns:json=""http://www.samplenamespace.com/json"">25</age>     |
|      |         | </details>                                                            |
|      |         | </Main>                                                               |
| 1004 | N       | <Main  xmlns:json=""http://www.samplenamespace.com/json"" >           |
|      |         | <ID json:ValueType=""Number"">1004</ID>                               |
|      |         | <details>                                                             |
|      |         | <name xmlns:json=""http://www.samplenamespace.com/json"">Kippy</name> |
|      |         | <age  xmlns:json=""http://www.samplenamespace.com/json"">26</age>     |
|      |         | </details>                                                            |
|      |         | </Main>                                                               |
+------+---------+-----------------------------------------------------------------------+

In this table, I want to alter the xml_data column to remove the http://www.samplenamespace.com/json namespaces to all the subnodes except <Main> node.

My query:

update #final --#final is my table
set xml_data.modify(replace('xmlns:json="http://www.samplenamespace.com/json"',' ','') where.... ) -- I don't know to access the root node here

Any help?

ccarpenter32
  • 1,058
  • 2
  • 9
  • 17
Rick
  • 1,392
  • 1
  • 21
  • 52

1 Answers1

1

Since you have two elements that have to be cleaned, you can use sql replace on the xml_data column to remove the namespace from the name element, then the age element. This will leave the main element as is with the required namespace.

How to replace a string in a SQL Server Table Column

create   TABLE [dbo].[foo]
(
cid int not null,
cidtype varchar(1) null,
xml_data varchar(max) null
)
go

insert into foo (cid,cidtype,xml_data) values
(1001,'N','<Main  xmlns:json=""http://www.samplenamespace.com/json"" >           
 <ID json:ValueType=""Number"">1001</ID>                               
 <details>                                                             
 <name xmlns:json=""http://www.samplenamespace.com/json"">John</name>  
 <age  xmlns:json=""http://www.samplenamespace.com/json"">12</age>     
 </details>                                                            
 </Main>')


 insert into foo (cid,cidtype,xml_data) values (1003, 'N',
 '<Main  xmlns:json=""http://www.samplenamespace.com/json"" >            
  <ID json:ValueType=""Number"">1003</ID>                                
  <details>                                                              
  <name xmlns:json=""http://www.samplenamespace.com/json"">Diane</name>  
  <age  xmlns:json=""http://www.samplenamespace.com/json"">25</age>      
  </details>                                                             
  </Main>')
  insert into foo (cid,cidtype,xml_data) values (1004,'N',
  '<Main  xmlns:json=""http://www.samplenamespace.com/json"" >            
 <ID json:ValueType=""Number"">1004</ID>                                
 <details>                                                              
 <name xmlns:json=""http://www.samplenamespace.com/json"">Kippy</name>  
 <age  xmlns:json=""http://www.samplenamespace.com/json"">26</age>      
 </details>                                                             
 </Main>')
 go
 select * from foo

 update foo
set xml_data = replace(xml_data, 'name xmlns:json=""http://www.samplenamespace.com/json""', 'name ')
go
update foo
set xml_data = replace(xml_data, 'age  xmlns:json=""http://www.samplenamespace.com/json""', 'age ')
go
select * from foo
L0uis
  • 703
  • 5
  • 8
  • This idea is working. However, on my sproc I have around 50 elements that needs to be updated. So out of 51 elements, I want only 1 element to have the namespace and updating 50 elements is so tedious. So I am trying to add a 'Where' condition to this. Is that possible? – Rick Jul 02 '18 at 18:27
  • 1
    You can replace all namespaces with blanks, then just add back the namespace to
    .
    – L0uis Jul 02 '18 at 18:50
  • Yes, I made the namespaces blank for all the fields and later added to the main node alone – Rick Jul 02 '18 at 19:51