Re: replace value of empty element?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



In the first case, when you use expression (/root/test/text())[1] you refer to
something that doesn't exist (there is no text node under element test) and
therefore nothing gets updated.

What you need to do is insert a new text node inder element "test" like this

declare @xml xml

set @xml = '<root><test></test></root>'

set @xml.modify('insert text{"test new value"} as first into (/root/test)[1] ')

select @xml


I hope this helps.

Denis Ruckebusch
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


<doctorphan@xxxxxxxxx> wrote in message
news:1177013246.440921.259810@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,

In SQL 2005, I have a XML with an one element with an empty value like
so:

<root>
<test></test>
</root>

When I try and replace the value of (/root/test) it does not seem to
work:

declare @xml xml
set @xml = '<root><test></test></root>'
set @xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
select @xml

returns this:
<root>
<test></test>
</root>


If however the element has a value already like so:

<root>
<test>some old value here already</test>
</root>

and I re-rerun the statement above:

declare @xml xml
set @xml = '<root><test></test></root>'
set @xml.modify('replace value of (/root/test/text())[1] with "test
new value"')
select @xml

returns this:
<root>
<test>test new value</test>
</root>

Is something wrong there?



.



Relevant Pages

  • Re: Illegal Charaters in path
    ... In the sample code I used the LoadXml first and got Data at the root ... Then I tried load, well I ... I have a simple XML ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: For XML problem
    ... DECLARE @sql NVARCHAR ... DECLARE @data XML; ... ROOT )'; ... where CustomerID Like 'A%' ...
    (microsoft.public.sqlserver.programming)
  • RE: For XML problem
    ... DECLARE @sql NVARCHAR ... DECLARE @data XML; ... ROOT )'; ... where CustomerID Like 'A%' ...
    (microsoft.public.sqlserver.programming)
  • Re: Sort xml?
    ... An XSLT stylesheet can sort XML documents, for instance if the input XML ...
    (microsoft.public.dotnet.xml)
  • Re: Result of query() method is always not null, then how?
    ... Use the XPath node() node test to see if there are any nodes in the document... ... declare @y xml ... The existin your example is the equivalent of existwhich matches the root of the document because that is the context in which it is being used. ...
    (microsoft.public.sqlserver.xml)