You do have a couple of options but may not be much better off than your three updates. Options are 1) reconstruct the XML or 2) create a mapping table.
DML operations on XML are expensive so there is a tipping point when reconstructing the XML may be more efficient - depending on the size of your XML, how many attributes are being replaced etc
The mapping table solution uses a loop so is similar to the three updates solution. However you do have the advantage of making it data-driven.
Anyway, have a look through this demo, let me know how you get on:
USE tempdb
GO
SET NOCOUNT ON
DECLARE @t TABLE ( yourXML XML )
INSERT INTO @t ( yourXML )
SELECT '<ENTITY NAME="entity1">
<ATTR ID="attr1" CAPTION="Attributes to Change" SIZE="100" WIDTH="100"></ATTR>
</ENTITY>'
SELECT 'before' s, * FROM @t
------------------------------------------------------------------------------------------------
-- Reconstruct the inner xml START
------------------------------------------------------------------------------------------------
-- Delete current element and attributes
UPDATE @t
SET yourXML.modify('delete ENTITY[@NAME="entity1"]/ATTR' )
DECLARE @xml XML = '', @caption VARCHAR(100), @size INT, @width INT
SELECT @caption = 'New Attribute Caption', @size = 200, @width = 300
-- Construct new element with attributes
SET @xml = @xml.query ( '<ATTR ID="attr1" CAPTION="{sql:variable("@caption")}" SIZE="{sql:variable("@size")}" WIDTH="{sql:variable("@width")}"></ATTR> ' )
-- Insert it back in
UPDATE t
SET yourXML.modify('insert sql:variable("@xml") into (ENTITY[@NAME="entity1"])[1] ')
FROM @t t
SELECT 'after' s, * FROM @t
-- Reconstruct the inner xml END
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-- Create mapping table and loop START
------------------------------------------------------------------------------------------------
DECLARE @i INT = 0
DECLARE @map TABLE ( attributeName VARCHAR(50) PRIMARY KEY, newValue VARCHAR(50) NOT NULL )
INSERT INTO @map ( attributeName, newValue ) VALUES ( 'CAPTION', 'New Attribute Caption 3' )
INSERT INTO @map ( attributeName, newValue ) VALUES ( 'SIZE', 123 )
INSERT INTO @map ( attributeName, newValue ) VALUES ( 'WIDTH', 456 )
SELECT 'before 2' s, * FROM @t
WHILE 1=1
BEGIN
SET @i += 1
-- Update the XML based on the mapping table
UPDATE @t
SET yourXML.modify('replace value of (/ENTITY/ATTR/@*[local-name()= sql:column("attributeName")])[1] with sql:column("newValue")')
FROM @t
CROSS JOIN @map m
WHERE yourXML.exist('(/ENTITY/ATTR/@*[local-name()= sql:column("attributeName")][. != sql:column("newValue")])') = 1
IF @@rowcount = 0 BREAK
-- Guard against infinite loop
IF @i > 99 BEGIN RAISERROR( 'Too many loops! %i', 16, 1, @i ) BREAK END
END
SELECT @i loops
SELECT 'after' s, * FROM @t
-- Create mapping table and loop END
------------------------------------------------------------------------------------------------