查询在SQL Server中删除XML中的一个标签。

问题描述 投票:0回答:1

我有一个xml,名字空间如下。我如何删除标签 <ns1:replyTo> 使用XML查询从XML中删除标签。我们可以用下面的查询来删除标签。我不知道如何通过标签与 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>
sql-server xml xquery-sql
1个回答
0
投票

你当前的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;

Which yields...

<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>
© www.soinside.com 2019 - 2024. All rights reserved.