1

I have have the following xml fragment in a column in SQL Server

<ul>
   <li><a id="cat" href="cat.html">Cat</a></li>
   <li><a id="dog" href="dog.html">Dog</a></li>

I want to delete the <li> tag that has an <a> tag with id="cat"

    DECLARE @id varchar(40) =  'cat'

    UPDATE  dbo.Pref
    SET     xmlPref.modify('delete /ul/li/a[@id=sql:variable("@id")]')
    WHERE   pref    = 1

Does not quite work as it leaves <li> around.

Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
James A Mohler
  • 11,060
  • 15
  • 46
  • 72

2 Answers2

2

remove the "a" element in the path of expression and add it to the condition

DECLARE @id varchar(40) =  'cat'

UPDATE dbo.Pref
SET xmlPref.modify('delete //ul/li[a/@id=sql:variable("@id")]')
WHERE pref = 1

See demo on SQLFiddle

Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
-1

what about using replace built in function within SQL Server

UPDATE dbo.Pref SET COLUMNNAME = REPLACE('EXPRESSION TO BE SEARCHED FOR','PART OF THE STRING TO BE SEARCHED','THE REPLACEMENT STRING') WHERE .......

Scott
  • 19
  • 2
  • 6