简体   繁体   中英

Update XML value with multiple namespaces not working

I have searched a lot of questions for this one but I can't find out what I am missing I have the following xml and I want to update the SubmissionFormId

在此处输入图像描述

I am using the following script but I can't understand why it doesn't work.

WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS i,'http://schemas.ersl.ie/regsys' AS xs)
update submission_form_schema
SET sub_form_xml.modify('  
  replace value of (/SubmissionForm/SubmissionFormId/text())[1]  
  with ("11519")')
where sub_form_id = 11519
select top 10* from submission_form_schema where sub_form_id = 11519

My table is like this

在此处输入图像描述

Any clues or hints on what I should be looking for would be a great help.

You could use defined xs: :

WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS i
                  ,'http://schemas.ersl.ie/regsys' AS xs)
update submission_form_schema
SET sub_form_xml.modify('  
  replace value of (/xs:SubmissionForm/xs:SubmissionFormId/text())[1]  
  with ("11519")')
where sub_form_id = 11519;

db<>fiddle demo


Altertatively using DEFAULT :

WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS i
                  ,DEFAULT 'http://schemas.ersl.ie/regsys')
update submission_form_schema
SET sub_form_xml.modify('  
  replace value of (/SubmissionForm/SubmissionFormId/text())[1]  
  with ("11519")')
where sub_form_id = 11519;

db<>fiddle demo2


Or even omit XMLNAMESPACES and use wildcard:

update submission_form_schema
SET sub_form_xml.modify('  
  replace value of (/*:SubmissionForm/*:SubmissionFormId/text())[1]  
  with ("11519")')
where sub_form_id = 11519;

db<>fiddle demo3

Can't see the whole xml but the obvious problem is you don't specify the defined prefixes in the replace value of expression. It should be

WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS i,'http://schemas.ersl.ie/regsys' AS xs)
update submission_form_schema
SET sub_form_xml.modify('  
  replace value of (/xs:SubmissionForm/xs:SubmissionFormId/text())[1]  
  with ("11519")')
where sub_form_id = 11519;

Demo:

create table submission_form_schema(
  sub_form_id int,
  sub_form_xml xml
);
insert submission_form_schema values
(11519, '<SubmissionForm xmlns="http://schemas.ersl.ie/regsys" xmlns:i="http://schemas.ersl.ie/regsys">
       <SubmissionFormId>22222</SubmissionFormId>
     </SubmissionForm>');

WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS i,'http://schemas.ersl.ie/regsys' AS xs)
update submission_form_schema
SET sub_form_xml.modify('  
  replace value of (/xs:SubmissionForm/xs:SubmissionFormId/text())[1]  
  with ("11519")')
where sub_form_id = 11519;

select *
from submission_form_schema;

Returns

sub_form_id sub_form_xml
------  --------
11519   <SubmissionForm xmlns="http://schemas.ersl.ie/regsys" xmlns:i="http://schemas.ersl.ie/regsys"><SubmissionFormId>11519</SubmissionFormId></SubmissionForm>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM