简体   繁体   English

从SQL Server中的XML删除所有空节点

[英]Delete all empty nodes from XML in SQL Server

I want to remove all empty nodes in a XML file. 我想删除XML文件中的所有空节点。 Even if the node is present as 即使该节点以

<Node/>    OR    <Node></Node>

node should be deleted from the XML. 节点应从XML中删除。

<Root type="1">
<A></A>
<B>
    <B1>
        <B12/>
        <B13/>
    </B1>
    <B2>
        123
        <B21></B21>
    </B2>
   <B3 type="3">
       <B4/>
   </B3>
</B>
<C/>
</Root>

Expected output: 预期产量:

<Root type="1">
<B>
    <B2>
        123
    </B2>
    <B3 type="3">
    </B3>
</B>
</Root>

Delete B1 node because all nodes under B1 is empty and also there is no attribute as well. 删除B1节点,因为B1下的所有节点都是空的,并且也没有属性。

Do not delete B2 because , B2 has a value 123 , but delete its empty child. 不要删除B2,因为,B2的值为123,而是删除其空子级。

Do not delete B3 because , B3 has an attribute, but delete its empty child. 不要删除B3,因为,B3具有属性,而是删除其空子级。

I am using SQL to do the same , but in case if this can be done in c# as well , I can call C# script from SSIS, but SQL will be preferred. 我正在使用SQL来执行相同的操作,但是如果也可以在c#中完成,则可以从SSIS调用C#脚本,但是首选SQL。

A way to do in C# would be: 使用C#的一种方法是:

var x = XElement.Parse(@"<Root type=""1"">
                            <A></A>
                            <B>
                                <B1>
                                    <B12/>
                                    <B13/>
                                </B1>
                                <B2>
                                    123
                                    <B21></B21>
                                </B2>
                               <B3 type=""3"">
                                   <B4/>
                               </B3>
                            </B>
                            <C/>
                            </Root>");

foreach(XElement child in x.Descendants().Reverse())
{
    if(!child.HasElements && string.IsNullOrEmpty(child.Value) && !child.HasAttributes) 
        child.Remove();
}

It can be done easily with regular expressions: 使用正则表达式可以轻松完成:

string xml = @"<Root type=""1"">
                < A ></ A >
                < B >
                    < B1 >
                        < B12 />
                        < B13 />
                    </ B1 >
                    < B2 >
                        123
                        < B21 ></ B21 >
                    </ B2 >
                   < B3 type = ""3"" >

                        < B4 />

                    </ B3 >
                 </ B >
                 < C />
                 </ Root > ";


xml = Regex.Replace(xml, @"<.+?/>", "");
xml = Regex.Replace(xml, @"<(.+?)>\s*</\1>", "");

The simplest way to do this in SQL Server . 在SQL Server中执行此操作的最简单方法。

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

I am also able to select all the nodes that I ignored/deleted. 我还可以选择所有我忽略/删除的节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM