SQL Server XML add attribute if non-existent -
i trying add attribute if not exist. should simple, new xml xpath/xquery/etc., excuse ignorance.
i want able pass xml data , modify it...
alter function [dbo].[convertxmldata](@xmldata xml) returns xml begin return @xmldata.<something here> end
if pass data like:
<something> sample data <xxx id="1"/> , <xxx id="2" runat="server" />. more <yyy id="3" /> </something>
i
<something> sample data <xxx id="1" runat="server" /> , <xxx id="2" runat="server" />. more <yyy id="3" /> </something>
and not :
<something> sample data <xxx id="1" runat="server" /> , <xxx id="2" runat="server" runat="server"/>. more <yyy id="3" /> </something>
you can do
set @xmldata.modify('insert attribute runat { "server" } descendant::xxx[not(@runat)][1]');
this change first xxx
descendant not having runat attribute, @ least sql server 2005 can modify 1 node @ time.
maybe combining above while helps e.g.
while @xmldata.exist('descendant::xxx[not(@runat)]') = 1 begin set @xmldata.modify('insert attribute runat { "server" } descendant::xxx[not(@runat)][1]'); end
i don't have access sql server 2008 r2 think modify still limited 1 node @ time try
alter function [dbo].[convertxmldata](@xmldata xml) returns xml begin while @xmldata.exist('descendant::xxx[not(@runat)]') = 1 begin set @xmldata.modify('insert attribute runat { "server" } descendant::xxx[not(@runat)][1]'); end return @xmldata end
Comments
Post a Comment