我有一个数据库表,其中一列名为'mapSQL'。
此列的值包含XML Document的nvarchar值。
存储在数据库中的值:
<root>
<node1 attr1="foobar">
<child1 attr1="foo">
</child1>
</node1>
<node2 attr2="hello">
</node2>
</root>
在从数据库中检索列时,我希望能够去掉一些节点的属性。在上面的例子中,我希望能够删除所有'attr1'属性及其值。
我希望返回SQL文档。请注意,所有'attr1'属性都消失了:
<root>
<node1>
<child1>
</child1>
</node1>
<node2 attr2="hello">
</node2>
</root>
我发现这可以使用REPLACE函数和REGEX,但是t-SQL在REPLACE函数中不支持REGEX。还有其他选择吗?
使用REGEX的解决方案:
REPLACE({0}.{1}, 'ToolTipResourseId=\"[^\"]*\"', '')
谢谢!
答案 0 :(得分:0)
尝试这样的smth:
declare @xml xml = N'<root>
<node1 attr1="foobar">
<child1 attr1="foo">
</child1>
</node1>
<node2 attr2="hello">
</node2>
</root>'
set @xml.modify('delete (//@*[local-name()="attr1"])')
select @xml
要进行选择,您可以创建获取xml值并返回已清除值的标量函数:
CREATE FUNCTION [dbo].[ClearXml]
(
@Xml xml
)
RETURNS xml
AS
BEGIN
set @Xml.modify('delete (//@*[local-name()="attr1"])')
return @Xml
END
create table #temp (value xml)
declare @xml xml = N'<root>
<node1 attr1="foobar">
<child1 attr1="foo">
</child1>
</node1>
<node2 attr2="hello">
</node2>
</root>'
insert into #temp
values (@xml)
select
dbo.ClearXml(t.value)
from #temp as t
drop table #temp