Заменить теги CSS из строки в SQL Server - PullRequest
0 голосов
/ 04 июля 2019

Мне нужно заменить

<span style="text-decoration: underline;"> 

на

и </span> на </u></span>

Только заменить </span> на <span style="text-decoration: underline;">

Нет необходимости заменять </span> на <span style="color: #9bbb59;">

 declare @text varchar(max)
 set @text = 'i want to how tags are
 <span style="color: #9bbb59;">
   <span style="text-decoration: underline;">working</span>
 </span>'

Ожидаемый результат:

I want to how tags are 
<span style="color: #9bbb59;">
 <span style="text-decoration: underline;">
   <u>working</u>
 </span>
</span>

Ответы [ 3 ]

0 голосов
/ 04 июля 2019

Не уверен, что это лучше или нет, но попробуйте выполнить запрос ниже:

DECLARE @MainString NVARCHAR(MAX) ='i want to how tags are <span style="color: #9bbb59;">
   <span style="text-decoration: underline;">I am testing for another string</span>
 </span>'

DECLARE @startIndex INT, @endIndex INT

SET @startIndex = CHARINDEX('<span style="text-decoration: underline;">',@MainString,CHARINDEX('<span style="text-decoration: underline;">',@MainString)) + 42
SET @endIndex = CHARINDEX('</span>',@MainString,CHARINDEX('<span style="text-decoration: underline;">',@MainString))

DECLARE @text NVARCHAR(MAX) = (SELECT SUBSTRING(@MainString,@startIndex,@endIndex - @startIndex))

SET @MainString = (SELECT REPLACE(@MainString, '<span style="text-decoration: underline;">','<span style="text-decoration: underline;"><u>'))

SET @MainString = ( SELECT REPLACE(@MainString, '<u>' + @text + '</span>','<u>' + @text + '</u></span>'))

SELECT @MainString

Что выводит как ожидалось:

i want to how tags are 
<span style="color: #9bbb59;">  
  <span style="text-decoration: underline;">
    <u>I am testing for another string</u>
  </span>
</span>
0 голосов
/ 04 июля 2019

Вы можете играть с функциями XML:

 DECLARE @text VARCHAR(MAX)
 SET @text = 'i want to how tags are
 <span style="color: #9bbb59;">
   <span style="text-decoration: underline;">working</span>
   <span style="text-decoration: underline;"><i>here</i></span>
   <span style="text-decoration: underline;">now</span>
 </span>';

 -- Convert to XML
 DECLARE @xml xml = CONVERT(XML, @text);

 -- Get the count of nodes to be updated
 DECLARE @nodeCount INT = @xml.value('count(//span[@style="text-decoration: underline;"])', 'int');
 DECLARE @i INT = 1;
 DECLARE @nodeValue XML;
 DECLARE @newValue XML;

 -- Iterate thru on the nodes
 WHILE (@i <= @nodeCount) BEGIN

    -- Get the original node value
    SET @nodeValue = @xml.query('(//span[@style="text-decoration: underline;"][sql:variable("@i")])/*')
    SET @nodeValue = IIF(CONVERT(NVARCHAR(MAX), @nodeValue) = N'', @xml.query('(//span[@style="text-decoration: underline;"][sql:variable("@i")])/text()'), @nodeValue)

    -- Create the new node value
    SET @newValue = N'<u></u>';
    SET @newValue.modify('
        insert sql:variable("@nodeValue")
        into (//u)[1]
    ');

    -- Remove child nodes
    SET @xml.modify('
        delete (//span[@style="text-decoration: underline;"][sql:variable("@i")]/*)
    ');

    -- Remove textual data
    SET @xml.modify('
        replace value of (//span[@style="text-decoration: underline;"][sql:variable("@i")]/text())[1]
        with ""
    ');

    -- Add the new value as child
    SET @xml.modify('
        insert sql:variable("@newValue")
        into (//span[@style="text-decoration: underline;"])[sql:variable("@i")][1]'
    );  

    SET @i = @i+1;
END;
SELECT @xml, CONVERT(NVARCHAR(MAX), @xml);

Обратите внимание, что это не будет работать, если исходный текст не может быть преобразован в XML.

0 голосов
/ 04 июля 2019

Вы можете попробовать это

SELECT REPLACE('i want to how tags are <span style="color: #9bbb59;"><span 
   style="text-decoration: underline;">working</span></span>?', '<span 
   style="text-decoration: underline;">working</span>', '<span style="text- decoration: underline;"><u>working</u></span>');

Я надеюсь, что это будет работать

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...