I tried various methods to get around this
- using with xmlnamespaces: no change
- using explicitly defined namespaces: no change
- using the same namespace as the parent: results in the default namespace being inserted into the Document node
- deleting the empty namespace with modify/delete: would not remove the xmlns attribute
- inserting the values dynamically inside the modify/insert : "The argument 1 of the XML data type method "modify" must be a string literal."
Solution
So the last error got me thinking, it will insert the node I want with no namespace so long as it's a string literal.. So I did just that.
- Insert empty node with empty attributes
- Use modify/replace to fill in the values of the attributes after insertion
And here's the example of what it looks like
declare @temp xml
set @temp = cast(
'<Catalog xmlns="http://schemas.example.com/stuff/stuff">
<String Key="Name" Tag="22a41320-bb66-41a9-8806-760d13679c6c">Document Title 1</String>
<String Key="Name" Tag="023463cf-9237-45b6-ac3f-621b9b09f609">Title for document 2</String>
</Catalog>' as xml)
select 'before', @temp
while (@temp.value('declare default element namespace "http://schemas.example.com/stuff/stuff"; count(/Catalog/String)', 'int') > 0)
begin
SET @temp.modify('declare default element namespace "http://schemas.example.com/stuff/stuff"; insert <Document Key="" Handle="" /> into (/Catalog)[1] ')
SET @temp.modify('declare default element namespace "http://schemas.example.com/stuff/stuff"; replace value of (/Catalog/Document[@Handle=""]/@Handle)[1] with (/Catalog/String/@Tag)[1]')
SET @temp.modify('declare default element namespace "http://schemas.example.com/stuff/stuff"; replace value of (/Catalog/Document[@Key=""]/@Key)[1] with (/Catalog/String/text())[1]')
SET @temp.modify('declare default element namespace "http://schemas.example.com/stuff/stuff"; delete (/Catalog/String)[1]')
end
select 'after', @temp