Function for renaming an element in SQL Server XML

。_饼干妹妹 提交于 2019-12-11 15:52:35

问题


I'm trying to create a function able to rename an element within XML columns.

The function should be something as

create function dbo.fnRenameElement(
    @doc xml 
    , @parentElementPath nvarchar(MAX)
    , @oldElementName nvarchar (255)
    , @newElementName nvarchar (255)
    )
returns ... (whatever, not really interested actually)

Something I would use in this way

Select 
    dbo.fnRenameElement(MyTable.MyXmlColumn, 'some/xml/path/', 'ElementNameToReplace', 'NewElementName')
Where
    ...

I had a look at this answer, I have a similar need but I also need to have newName, OldName and xmlPath as variable, which I'm not able to work out.

It seems (am I wrong?) that I cannot use string variables withing query, modify, insert XQuery functions

I want to avoid any "convert to string - replace old with new element name - convert back to xml": this does not work if "oldElementName" can appear anywhere else within the xml column data

来源:https://stackoverflow.com/questions/59072683/function-for-renaming-an-element-in-sql-server-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!