If you are trying to replace the value of an element that does not exist if can be problematic, i have not found a nice way to do this, if any one does please let me know.
So to get from
<blob><a href=”microsoft.com”/><span>Microsoft</span></blob>
to
<blob>
<a href=”microsoft.com”>Microsoft</a>
</blob>
I’ve used this piece of very clunky code.
DECLARE @xml XML = N'<blob><a href="microsoft.com"/><span>Microsoft</span></blob>'
SET @xml.modify('
insert <a href="empty">Empty</a> into (/blob)[1]')
SET @xml.modify('replace value of (/blob/a/text())[1] with (/blob/span/text())[1]')
SET @xml.modify('replace value of (/blob/a/@href)[2] with (/blob/a/@href)[1]')
SET @xml.modify('delete (/blob/a)[1]')
SET @xml.modify('delete (/blob/span)[1]')
SELECT @xml
Many thanks for your help! I got it down to 2 modify operations:
declare @xml xml=’Microsoft’
SET @xml.modify(‘insert text{(/blob/span/text())[1]} into (/blob/a)[1]’)
SET @xml.modify(‘delete (/blob/span)[1]’)
SELECT @xml
That’s really not that clunky!
Your solution is a much cleaner approach. I could not get the syntax right for the two statement method. š