Good article. I've noticed that the "modify" function can only contain one operation per "SET". However, I want to delete multiple nodes in a piece of XML and not have to code several "SET @x.modify(...)" statements. SQL Server will not let you do "Set-based" operations multiple times on an XML column. So, maybe you can update your article with a way to delete multiple pieces of XML within the document at one time.
A table stores "Address" data in an Xml column called "Data". The xml is untyped but generally follows the following "schema":
However, the programmers have the authority to add any other Nodes they'd like in order to store other things about an address (ex. maybe the "Lat" and "Long" or the CASS Result [USPS Coding Accuracy Support System]). Any stored procedures or queries which need to update this XML data should NEVER remove any of the other nodes that they are not responsible for. In other words, do not simply overwrite the XML data, but rather just manipulate the nodes that are specific to the process at hand.
The solution to this (because one developer from another will never know ALL the nodes that will exist in the data at any time) is to simply "delete" all the nodes that that developer knows pertains to his process, and then "insert" them back in. This way, if the nodes didn't exist to begin with, then there will be no harm. This will be much cleaner than using "replace value of" functions which rely on the nodes existing up-front (which may not be the case as time goes on and more is added to the XML).
-- Get initial data from record
DECLARE @Data xml
SET @Data = '<Data type="address">
<Line1>123 Seseame St</Line1>
<OtherData>My other data</OtherData>
-- Remove known nodes
delete (//*[local-name()=("Line1", "Line2", "Line3", "City", "Region", "PostalCode", "PostalExtension")])
-- Insert known nodes with updated data
@Line1 nvarchar(50) = '999 Somewhere Ave',
@Line2 nvarchar(50) = NULL,
@Line3 nvarchar(50) = NULL,
@City nvarchar(50) = 'Walawala',
@Region nvarchar(50) = 'WA',
@PostalCode nvarchar(50) = '99999'
DECLARE @Nodes xml
SET @Nodes = ISNULL('<Line1>' + @Line1 + '</Line1>', '')
+ ISNULL('<Line2>' + @Line2 + '</Line2>', '')
+ ISNULL('<Line3>' + @Line3 + '</Line3>', '')
+ ISNULL('<City>' + @City + '</City>', '')
+ ISNULL('<Region>' + @Region + '</Region>', '')
+ ISNULL('<PostalCode>' + @PostalCode + '</PostalCode>', '')