1

I tried the following ways and could not find a way to replace empty node value without deleting it and without using XML attributes.

Replace node is not available in MS-SQL.

DECLARE @doc xml
SET @doc = '
    <f1>1</f1> 
    <f2/>
'
print '1) ' + convert(varchar(4000),@doc)


SET @doc.modify('
    replace value of (/f1/text())[1]
        with (''v2'')
')
print '2) ' + convert(varchar(4000),@doc)


SET @doc.modify('
    replace value of (/f1/text())[1]
        with (''v3'')
')
print '3) ' + convert(varchar(4000),@doc)


SET @doc.modify('
    replace value of (/f2/text())[1]
        with (''v4'')
')
print '4) ' + convert(varchar(4000),@doc)   + ' did not replace f2 value'


begin try
    exec sp_executesql N'
        SET @doc.modify(''
                replace value of (/f2)[1] with (''''v5'''')
        '')
        print ''5) '' + convert(varchar(4000),@doc)
    ',N'@doc xml',@doc
end try begin catch
    print '5) ' + error_message()
end catch


begin try
    exec sp_executesql N'
        SET @doc.modify(''
            replace value of (/f2/node())[1]
                with (''''v6'''')
        '')
        print ''6) '' + convert(varchar(4000),@doc)
    ',N'@doc xml',@doc
end try begin catch
    print '6) ' + error_message()
end catch


begin try
    exec sp_executesql N'
        SET @doc.modify(''
            replace value of (/f2)[1]
                with (''''<f2>v7/f2>'''')
        '')
        print ''7) '' + convert(varchar(4000),@doc)
    ',N'@doc xml',@doc
end try begin catch
    print '7) ' + error_message()
end catch


SET @doc.modify('
    delete (/f2)
')
SET @doc.modify('
    insert <f2>v8</f2> as last
        into (.)[1]
')
print '8) ' + convert(varchar(4000),@doc) + ' only delete and add works'

/*
    output:
        1) <f1>1</f1><f2/>
        2) <f1>v2</f1><f2/>
        3) <f1>v3</f1><f2/>
        4) <f1>v3</f1><f2/> did not replace f2 value
        5) XQuery [modify()]: The target of 'replace value of' must be a non-metadata attribute or an element with simple typed content, found 'element(f2,xdt:untyped) ?'
        6) XQuery [modify()]: The target of 'replace value of' cannot be a union type, found '(element(*,xdt:untyped) | comment | processing-instruction | text) ?'.
        7) XQuery [modify()]: The target of 'replace value of' must be a non-metadata attribute or an element with simple typed content, found 'element(f2,xdt:untyped) ?'
        8) <f1>v3</f1><f2>v8</f2> only delete and add works
*/
2
  • Tweaked the subject line to make the implementation more visible, since it's rather critical to the question. (I could answer this for an implementation supporting XQUF, but MS... shrug). Commented Mar 14, 2014 at 22:31
  • Possible duplicate of element() vs. node() in XQuery Commented Aug 16, 2016 at 20:36

1 Answer 1

1

Well I think something like this would do it

DECLARE @doc xml
SET @doc = '
    <f1>1</f1> 
    <f2/>
'
print '1) ' + convert(varchar(4000),@doc)


SET @doc.modify('
    insert text{"v9"} into (/f2)[1]
')
print '2) ' + convert(varchar(4000),@doc)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.