在 SQL 服务器中删除 XML 中的标签的查询

Query to remove a tag in XML in SQL Server

我有一个名为 space 的 xml,如下所示。如何使用 XML 查询从 XML 中删除标签 <ns1:replyTo>。我们可以使用类似以下查询的内容删除标签吗?我不确定如何使用 SOAP-ENV

传递标签
update tablename 
set XMLColumnname.modify(('delete /ServiceIn/file/sender/replyTo[1]'))

XML:

<?xml version="1.0"?>
    <SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/1999/XMLSchema"
        xmlns:si="http://someservice.abc.com/stds/xyz/a1"
        xmlns:oas="http://docs.oasis-open.org/abc/2004/01/oasis-200401-abc-abcsecurity-xyz- 1.0.xsd">
        <SOAP-ENV:Header>
            <oas:Security>
                <oas:Credential>
                    <oas:name>%s</oas:name>
                    <oas:Password>%s</oas:Password>
                </oas:Credential>
            </oas:Security>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <ns1:execute
                xmlns:ns1="urn:com.company.us.abc.service.xyz.someService">
                <si:ServiceRequestInfo>
                    <si:Tag1 />
                    <si:Tag2 />
                    <si:Tag3 />
                    <si:Tag4 />
                </si:ServiceRequestInfo>
                <ns1:ServiceIn>
                    <ns1:Tag5>%s</ns1:Tag5>
                    <ns1:File>
                        <ns1:Sender>
                            <ns1:Name>%s</ns1:Name>
                            <ns1:Email>%s</ns1:Email>
                            <ns1:replyTo>%s</ns1:replyTo>
                        </ns1:Sender>
                    </ns1:File>
                </ns1:ServiceIn>
            </ns1:execute>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

您当前的 XQuery 路径是匿名的,并且从错误的根目录开始。

您需要使用 with xmlnamespaces 定义所需的名称空间或在 XQuery 语句本身内声明名称空间,例如:

create table tablename (
  XMLColumnname xml
);

insert tablename values
(N'<?xml version="1.0"?>
    <SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/1999/XMLSchema"
        xmlns:si="http://someservice.abc.com/stds/xyz/a1"
        xmlns:oas="http://docs.oasis-open.org/abc/2004/01/oasis-200401-abc-abcsecurity-xyz- 1.0.xsd">
        <SOAP-ENV:Header>
            <oas:Security>
                <oas:Credential>
                    <oas:name>%s</oas:name>
                    <oas:Password>%s</oas:Password>
                </oas:Credential>
            </oas:Security>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <ns1:execute
                xmlns:ns1="urn:com.company.us.abc.service.xyz.someService">
                <si:ServiceRequestInfo>
                    <si:Tag1 />
                    <si:Tag2 />
                    <si:Tag3 />
                    <si:Tag4 />
                </si:ServiceRequestInfo>
                <ns1:ServiceIn>
                    <ns1:Tag5>%s</ns1:Tag5>
                    <ns1:File>
                        <ns1:Sender>
                            <ns1:Name>%s</ns1:Name>
                            <ns1:Email>%s</ns1:Email>
                            <ns1:replyTo>%s</ns1:replyTo>
                        </ns1:Sender>
                    </ns1:File>
                </ns1:ServiceIn>
            </ns1:execute>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>');

update tablename    
set XMLColumnname.modify('declare namespace soap="http://schemas.xmlsoap.org/soap/envelope/";
declare namespace foo="urn:com.company.us.abc.service.xyz.someService";
delete /soap:Envelope/soap:Body/foo:execute/foo:ServiceIn/foo:File/foo:Sender/foo:replyTo[1]');

select * from tablename;

产生...

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/1999/XMLSchema" 
    xmlns:si="http://someservice.abc.com/stds/xyz/a1" 
    xmlns:oas="http://docs.oasis-open.org/abc/2004/01/oasis-200401-abc-abcsecurity-xyz- 1.0.xsd">
    <SOAP-ENV:Header>
        <oas:Security>
            <oas:Credential>
                <oas:name>%s</oas:name>
                <oas:Password>%s</oas:Password>
            </oas:Credential>
        </oas:Security>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:execute xmlns:ns1="urn:com.company.us.abc.service.xyz.someService">
            <si:ServiceRequestInfo>
                <si:Tag1 />
                <si:Tag2 />
                <si:Tag3 />
                <si:Tag4 />
            </si:ServiceRequestInfo>
            <ns1:ServiceIn>
                <ns1:Tag5>%s</ns1:Tag5>
                <ns1:File>
                    <ns1:Sender>
                        <ns1:Name>%s</ns1:Name>
                        <ns1:Email>%s</ns1:Email>
                    </ns1:Sender>
                </ns1:File>
            </ns1:ServiceIn>
        </ns1:execute>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>