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
Leave a Reply