从 SQL 服务器中的字符串替换 CSS 标签
Replace CSS tags from string in SQL Server
我需要更换
<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>
你可以试试这个
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>');
希望有用
不确定是否最好,但请尝试以下查询:
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>
您可以使用 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,这将不起作用。
我需要更换
<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>
你可以试试这个
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>');
希望有用
不确定是否最好,但请尝试以下查询:
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>
您可以使用 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,这将不起作用。