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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -