简体   繁体   中英

Update XML attribute in SQL Server with name spaces

I have a table with xml column 'Data'. my requirement is to change attributes Uri and InstanceName as we promote the code. I wrote below update statements to do the job. When I execute I see 1 Row(s) affected but unfortunately it is not updating anything. Any help will be highly appreciated. Thanks!.

CREATE TABLE [dbo].[Test_TBD](
    [Id] [int] NULL,
    [Data] [xml] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO   

 WITH XMLNAMESPACES(DEFAULT 'http://www.tech.com/datafeed/dfx/2010/04')
 UPDATE Test_TBD set
Data.modify('replace value of (/DataFeed/Transporter/ArcherWebServiceTransportActivity/@Uri)[1] with "https://arcs-x"');

 UPDATE Test_TBD set
 Data.modify('replace value of (/DataFeed/Transporter/ArcherWebServiceTransportActivity/@InstanceName)[1] with "ARCS-X"');

Here is the sample data in Data column.

'<DataFeed xmlns="http://www.tech.com/datafeed/dfx/2010/04" xmlns:plugin="pluginExtensions" Type="TODO" Guid="TODO" UserAccount="DF_LEAN_PopulateCommentsSubForm" Locale="en-US" DateFormat="" ThousandSeparator="" NegativeSymbol="" DecimalSymbol="" SendingNotifications="false" SendJobStatusNotifications="false" RecipientUserIds="" RecipientGroupIds="" RecipientEmailAddresses="" Name="CI_C11.01_Lean-Lean_Reject Comments_A2A" >
      <Transporter>
        <transporters:ArcherWebServiceTransportActivity xmlns:transporters="clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed" xmlns:out="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:compModel="clr-namespace:ArcherTech.DataFeed.ComponentModel;assembly=ArcherTech.DataFeed" xmlns:channel="clr-namespace:ArcherTech.DataFeed.Engine.Channel;assembly=ArcherTech.DataFeed" xmlns:engine="clr-namespace:ArcherTech.DataFeed.Engine;assembly=ArcherTech.DataFeed" xmlns:kernel="clr-namespace:ArcherTech.Kernel.Channel;assembly=ArcherTech.Kernel" xmlns="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:schema="clr-namespace:System.Xml.Schema;assembly=System.Xml" xmlns:xmlLinq="clr-namespace:System.Xml.Linq;assembly=System.Xml" xmlns:domain="clr-namespace:ArcherTech.Common.Domain;assembly=ArcherTech.Common" xmlns:s="clr-namespace:System;assembly=mscorlib" x:Key="transportActivity" SearchType="ReportId" Uri="https://arcs-d" RecordsPerFile="100" ReportID="EC514865-88D5-49CE-A200-7769EC1C2A88" UseWindowsAuth="false" IsWindowsAuthSpecific="false" WindowsAuthUserName="i9XzCczAQ7J2rHwkg6wG9QF8+O9NCYJZP6y5Kzw4be0+cdvUaGu/9+rHuLstU736pnQrRcwmnSIhd6oPKIvnLA==" WindowsAuthPassword="+y0tCAKysxEMSGv1unpHxfg6WjH5XWylgP45P5MLRdQ6+zAdOLSVy7s3KJa3+9j2i83qn8I8K7+1+QBlCJT1E7sLQHWRFOCEdJgXaIr1gWfUEO+7kjuJnZcIEKZJa2wHyqc2Z08J2SKfdCLh7HoLtg==" WindowsAuthDomain="" ProxyName="" ProxyPort="8080" ProxyUsername="" ProxyPassword="" ProxyDomain="" IsProxyActive="False" ProxyOption="None" InstanceName="ARCS-D" TempFileOnSuccessAction="DoNothing" TempFileOnSuccessRenameString="" TempFileOnErrorAction="DoNothing" TempFileOnErrorRenameString="" Transform="{engine:DataFeedBinding Path=Transform}" SessionContext="{engine:DataFeedBinding Path=Session}">
          <transporters:ArcherWebServiceTransportActivity.Credentials>
            <NetworkCredentialWrapper UserName="TeSZmI1SqO0eJ0G2nDVU+glFg/9eZfeMppYQnPfbeg8=" Password="Slt4VHqjkYscWyCwZK40QJ7KOQroG9OTKr+RGt9bQjE=" />
          </transporters:ArcherWebServiceTransportActivity.Credentials>
        </transporters:ArcherWebServiceTransportActivity>
      </Transporter> 
    </DataFeed>'

You need to use WITH XMLNAMESPACES in both your update statements and you need to use the namespace
clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed
for the transporters:ArcherWebServiceTransportActivity node.

WITH XMLNAMESPACES(DEFAULT 'http://www.tech.com/datafeed/dfx/2010/04',
                   'clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed' as t)
UPDATE Test_TBD set
Data.modify('replace value of (/DataFeed/Transporter/t:ArcherWebServiceTransportActivity/@Uri)[1] with "https://arcs-x"');

WITH XMLNAMESPACES(DEFAULT 'http://www.tech.com/datafeed/dfx/2010/04',
                   'clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed' as t)
UPDATE Test_TBD set
Data.modify('replace value of (/DataFeed/Transporter/t:ArcherWebServiceTransportActivity/@InstanceName)[1] with "ARCS-X"');

Just for completness, as you have found the answer WITH XMLNAMESPACES already, there are other approaches as well (but admittedly I would go with Mikaels suggestion)

DECLARE @xml XML=
'<DataFeed xmlns="http://www.tech.com/datafeed/dfx/2010/04" xmlns:plugin="pluginExtensions" Type="TODO" Guid="TODO" UserAccount="DF_LEAN_PopulateCommentsSubForm" Locale="en-US" DateFormat="" ThousandSeparator="" NegativeSymbol="" DecimalSymbol="" SendingNotifications="false" SendJobStatusNotifications="false" RecipientUserIds="" RecipientGroupIds="" RecipientEmailAddresses="" Name="CI_C11.01_Lean-Lean_Reject Comments_A2A" >
      <Transporter>
        <transporters:ArcherWebServiceTransportActivity xmlns:transporters="clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed" xmlns:out="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:compModel="clr-namespace:ArcherTech.DataFeed.ComponentModel;assembly=ArcherTech.DataFeed" xmlns:channel="clr-namespace:ArcherTech.DataFeed.Engine.Channel;assembly=ArcherTech.DataFeed" xmlns:engine="clr-namespace:ArcherTech.DataFeed.Engine;assembly=ArcherTech.DataFeed" xmlns:kernel="clr-namespace:ArcherTech.Kernel.Channel;assembly=ArcherTech.Kernel" xmlns="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:schema="clr-namespace:System.Xml.Schema;assembly=System.Xml" xmlns:xmlLinq="clr-namespace:System.Xml.Linq;assembly=System.Xml" xmlns:domain="clr-namespace:ArcherTech.Common.Domain;assembly=ArcherTech.Common" xmlns:s="clr-namespace:System;assembly=mscorlib" x:Key="transportActivity" SearchType="ReportId" Uri="https://arcs-d" RecordsPerFile="100" ReportID="EC514865-88D5-49CE-A200-7769EC1C2A88" UseWindowsAuth="false" IsWindowsAuthSpecific="false" WindowsAuthUserName="i9XzCczAQ7J2rHwkg6wG9QF8+O9NCYJZP6y5Kzw4be0+cdvUaGu/9+rHuLstU736pnQrRcwmnSIhd6oPKIvnLA==" WindowsAuthPassword="+y0tCAKysxEMSGv1unpHxfg6WjH5XWylgP45P5MLRdQ6+zAdOLSVy7s3KJa3+9j2i83qn8I8K7+1+QBlCJT1E7sLQHWRFOCEdJgXaIr1gWfUEO+7kjuJnZcIEKZJa2wHyqc2Z08J2SKfdCLh7HoLtg==" WindowsAuthDomain="" ProxyName="" ProxyPort="8080" ProxyUsername="" ProxyPassword="" ProxyDomain="" IsProxyActive="False" ProxyOption="None" InstanceName="ARCS-D" TempFileOnSuccessAction="DoNothing" TempFileOnSuccessRenameString="" TempFileOnErrorAction="DoNothing" TempFileOnErrorRenameString="" Transform="{engine:DataFeedBinding Path=Transform}" SessionContext="{engine:DataFeedBinding Path=Session}">
          <transporters:ArcherWebServiceTransportActivity.Credentials>
            <NetworkCredentialWrapper UserName="TeSZmI1SqO0eJ0G2nDVU+glFg/9eZfeMppYQnPfbeg8=" Password="Slt4VHqjkYscWyCwZK40QJ7KOQroG9OTKr+RGt9bQjE=" />
          </transporters:ArcherWebServiceTransportActivity.Credentials>
        </transporters:ArcherWebServiceTransportActivity>
      </Transporter> 
    </DataFeed>';


SELECT 'original: ' + @xml.value('(/*:DataFeed/*:Transporter/*:ArcherWebServiceTransportActivity/@Uri)[1]','varchar(max)') + ' / ' + @xml.value('(/*:DataFeed/*:Transporter/*:ArcherWebServiceTransportActivity/@InstanceName)[1]','varchar(max)');

--namespaces declared as part of the XML_DML
SET @xml.modify('declare namespace ns="http://www.tech.com/datafeed/dfx/2010/04";
                 declare namespace t="clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed"; 
                 replace value of (/ns:DataFeed/ns:Transporter/t:ArcherWebServiceTransportActivity/@Uri)[1] with "https://arcs-x"');

--The Uri is changed
SELECT 'declared: ' + @xml.value('(/*:DataFeed/*:Transporter/*:ArcherWebServiceTransportActivity/@Uri)[1]','varchar(max)') + ' / ' + @xml.value('(/*:DataFeed/*:Transporter/*:ArcherWebServiceTransportActivity/@InstanceName)[1]','varchar(max)');

--namespace tricked out with the "*"
SET @xml.modify('replace value of (/*:DataFeed/*:Transporter/*:ArcherWebServiceTransportActivity/@InstanceName)[1] with "ARCS-X"');

--The InstanceName changed
SELECT 'asterisk: ' + @xml.value('(/*:DataFeed/*:Transporter/*:ArcherWebServiceTransportActivity/@Uri)[1]','varchar(max)') + ' / ' + @xml.value('(/*:DataFeed/*:Transporter/*:ArcherWebServiceTransportActivity/@InstanceName)[1]','varchar(max)');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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