TSQL modify()方法更改xml元素的前缀

时间:2014-04-02 05:21:52

标签: xml sql-server-2008 tsql

我有以下XML:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Body>
            <soapenv:Fault xmlns:m="http://schemas.xmlsoap.org/soap/envelope/">
                <first>value1</first>
                <last>value2</last>
            </soapenv:Fault>
        </soapenv:Body>
    </soapenv:Envelope>

我想要做的就是使用下面的代码使用给定的XPATH(忽略前缀)更新<first>元素的值:

     SET ANSI_PADDING ON;            
     SET ANSI_WARNINGS ON;            
     declare @xmlTBL table(tmlXML xml);             
     insert into @xmlTBL(tmlXML)            
     values('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <soapenv:Fault xmlns:m="http://schemas.xmlsoap.org/soap/envelope/"> <first>valuu1</first> <last>value2</last> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>');  

    UPDATE @xmlTBL SET tmlXML.modify('insert text{"TEST"}  into (/*:Envelope/*:Body/*:Fault/first[1] [not(node()) and not(text())])[1]'); 

元素值的更新有效但是正在发生的是元素<soapenv:Fault>现在变为<m:Fault>,如下所示:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Body>
            <m:Fault xmlns:m="http://schemas.xmlsoap.org/soap/envelope/">
                <first>TEST</first>
                <last>value2</last>
            </m:Fault>
        </soapenv:Body>
    </soapenv:Envelope>

在没有前缀生效的情况下,是否可以存档我正在做的事情?

1 个答案:

答案 0 :(得分:0)

与此比较:

declare @xmlOriginal xml = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <soapenv:Fault xmlns:m="urn:SomeEntity/1.0">
      <faultcode>a</faultcode>
      <faultstring>b</faultstring>
      <faultactor>c</faultactor>
    </soapenv:Fault>
  </soapenv:Body>
</soapenv:Envelope>'

declare @xml xml = @xmlOriginal

set @xml.modify('insert text{"TEST"} into (/*:Envelope/*:Body/*:Fault/faultcode[1])[1]')

select @xml, @xmlOriginal

请注意,名称空间定义不同。