Azure Sql:无法替换 HTML 字符串
Azure Sql : Unable to Replace HTML String
我在数据库中有以下 html
<table>
<tbody>
<tr>
<td>
<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<!-- /button -->
<!-- spacing -->
<tr>
<td width="100%" height="15"></td>
</tr>
</tbody>
</table>
并且我正在尝试附加额外的 table 行以使 html 如下所示
<tr>
<td>
<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__" target="_blank">notification.templates.key</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
当我如下所示手动替换 html
时,它工作正常。
DECLARE @html varchar(MAX) = '<table>
<tbody>
<tr>
<td>
<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<!-- /button -->
<!-- spacing -->
<tr>
<td width="100%" height="15"></td>
</tr>
</tbody>
</table>'
SELECT @html = REPLACE(@html,
'<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>'
,'<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__" target="_blank">notification.templates.key</a>
</span>
</td>
</tr>
</tbody>')
print @html
但是当我尝试直接在 db colmn 的 html 上进行操作时,如下所示,它在
处不起作用
DECLARE @html VARCHAR(MAX) = (SELECT top 1 BodyText
FROM [MessageTemplate]
WHERE body LIKE '%<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">%')
SELECT @html = REPLACE(@html,
'<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>'
,'<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__" target="_blank">notification.templates.key</a>
</span>
</td>
</tr>
</tbody>')
print @html
有人可以帮我解决这个问题吗?
请尝试以下解决方案。
它将HTML视为(X)HTML,即XML。
在那之后 MS SQL 服务器的 XQuery 通过 .modify()
方法使它变得容易。
SQL
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, BodyText XML);
INSERT INTO @tbl (BodyText) VALUES
(N'<table>
<tbody>
<tr>
<td>
<table height="36" align="center" valign="middle" border="0"
cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle"
height="36"
style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;"
href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<!-- /button -->
<!-- spacing -->
<tr>
<td width="100%" height="15"></td>
</tr>
</tbody>
</table>');
DECLARE @newTR XML =
N'<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__"
target="_blank">notification.templates.key</a>
</span>
</td>
</tr>';
DECLARE @html XML = (SELECT BodyText FROM @tbl);
-- before
SELECT @html;
SET @html.modify('insert sql:variable("@newTR") into (/table/tbody/tr/td/table/tbody)[1]');
-- after
SELECT @html;
我在数据库中有以下 html
<table>
<tbody>
<tr>
<td>
<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<!-- /button -->
<!-- spacing -->
<tr>
<td width="100%" height="15"></td>
</tr>
</tbody>
</table>
并且我正在尝试附加额外的 table 行以使 html 如下所示
<tr>
<td>
<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__" target="_blank">notification.templates.key</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
当我如下所示手动替换 html
时,它工作正常。
DECLARE @html varchar(MAX) = '<table>
<tbody>
<tr>
<td>
<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<!-- /button -->
<!-- spacing -->
<tr>
<td width="100%" height="15"></td>
</tr>
</tbody>
</table>'
SELECT @html = REPLACE(@html,
'<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>'
,'<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__" target="_blank">notification.templates.key</a>
</span>
</td>
</tr>
</tbody>')
print @html
但是当我尝试直接在 db colmn 的 html 上进行操作时,如下所示,它在
处不起作用 DECLARE @html VARCHAR(MAX) = (SELECT top 1 BodyText
FROM [MessageTemplate]
WHERE body LIKE '%<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">%')
SELECT @html = REPLACE(@html,
'<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>'
,'<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__" target="_blank">notification.templates.key</a>
</span>
</td>
</tr>
</tbody>')
print @html
有人可以帮我解决这个问题吗?
请尝试以下解决方案。
它将HTML视为(X)HTML,即XML。
在那之后 MS SQL 服务器的 XQuery 通过 .modify()
方法使它变得容易。
SQL
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, BodyText XML);
INSERT INTO @tbl (BodyText) VALUES
(N'<table>
<tbody>
<tr>
<td>
<table height="36" align="center" valign="middle" border="0"
cellpadding="0" cellspacing="0" class="tablet-button">
<tbody>
<tr>
<td width="auto" align="center" valign="middle"
height="36"
style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
<span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
<a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;"
href="__ConfirmLink__" target="_blank">Confirm</a>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<!-- /button -->
<!-- spacing -->
<tr>
<td width="100%" height="15"></td>
</tr>
</tbody>
</table>');
DECLARE @newTR XML =
N'<tr style="visibility: __displayProp__">
<td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
<span style="color: #FFFFFF; font-weight: 300;">
<a style="color: #4d4d4d; text-align: center;" href="__CancelLink__"
target="_blank">notification.templates.key</a>
</span>
</td>
</tr>';
DECLARE @html XML = (SELECT BodyText FROM @tbl);
-- before
SELECT @html;
SET @html.modify('insert sql:variable("@newTR") into (/table/tbody/tr/td/table/tbody)[1]');
-- after
SELECT @html;