使用名称空间更新SQL服务器中的XML属性

时间:2023-01-31 00:13:14

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!.

我有一个包含xml列“Data”的表。我的要求是在我们推广代码时更改属性Uri和InstanceName。我在下面写了update语句来完成这项工作。当我执行时,我看到1行受到影响,但不幸的是它没有更新任何东西。非常感谢您的帮助。谢谢!。

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>'

2 个解决方案

#1


2  

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.

您需要在更新语句中使用XMLNAMESPACES,并且需要使用名称空间clr-namespace: archertech.datafe. activities.transporters . assembly=ArcherTech。传输器的数据:ArcherWebServiceTransportActivity节点。

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"');

#2


2  

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)

正如您已经找到了XMLNAMESPACES的答案一样,这里还有其他方法(但必须承认,我支持Mikaels的建议)

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)');

#1


2  

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.

您需要在更新语句中使用XMLNAMESPACES,并且需要使用名称空间clr-namespace: archertech.datafe. activities.transporters . assembly=ArcherTech。传输器的数据:ArcherWebServiceTransportActivity节点。

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"');

#2


2  

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)

正如您已经找到了XMLNAMESPACES的答案一样,这里还有其他方法(但必须承认,我支持Mikaels的建议)

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)');