这将适用于 sql server。我已经包含了一些示例 img 标签来展示它是如何工作的。
但请注意,它会重新格式化 html,这可能并不理想。它将是相同的 html,但没有额外的空格和换行符。这可能不是你想要的。
我必须这样做的原因是当我转换为 xml 然后找到节点时,结果会被修剪掉额外的空白,并且无法在原始字符串中找到它们。如果您的 html 在 img 标签内没有多余的空格,那么您可以省略这一步:
select convert(varchar(max),convert(xml,x)) as trimmed from @t
如果这对您不起作用,那么希望匹配节点的识别至少会有所帮助。只需在匹配的 cte 后添加 select * from matching 即可查看您有什么。
它还可以在逐个文档的基础上工作,因此您可能必须将其置于循环中才能处理您的文档。或者更新它以跨批次工作,但我猜这是一次性操作,因此可能不需要。
declare @t table(x varchar(max))
insert @t values ('<html><div><img width="50%" /><img width="100%" src="foo" alt="bar" />
<span class="c">sometext</span>
<div><img src="foo" alt="bah" width="100%" /></div></div>
</html>')
;with [xml] as (
--- convert the string to xml
select convert(xml,x) as x from @t
)
,matching as (
-- Find all the img nodes that have width=100%
select convert(varchar(max),c.query('.')) as matches
from [xml]
cross apply x.nodes('//img[@width="100%"]') as t(c)
)
,source as (
-- Tidy up the source, as it has multiple spaces that prevent the matches from finding the nodes
select convert(varchar(max),convert(xml,x)) as trimmed from @t
)
,replaced as (
-- Work through recursively removing the 100% from matching nodes
select trimmed from source
union all
select replace(trimmed,matches,replace(matches,'width="100%"','')) as replaced from matching
cross join replaced
where charindex(matches,replaced.trimmed)>0
)
-- Get the shortest (or last) string from above
select top 1 * from replaced order by len(trimmed)
输出:
<html><div><img width="50%"/><img src="foo" alt="bar"/><span class="c">sometext</span><div><img src="foo" alt="bah" /></div></div></html>