Replace the XML element with new Element in SQL server

别等时光非礼了梦想. 提交于 2019-12-23 01:42:08

问题


Replace <apipAccessbility> element of this content with a new element in multiple rows with a column of type XML:

<Item title="1234" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">
    <ItemBody>xyz</ItemBody>
    <apipAccessibility xmlns="http://www.imsglobal.org/xsd/apip/apipv1p0/imsapip_qtiv1p0">
        <accessibilityInfo>
        Blablah
        </accessibilityInfo>
    </apipAccessibility>
</Item>

Once element is updated it should look like this:

<Item title="1234" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">
    <ItemBody>xyz</ItemBody>
    <apipAccessibility xmlns="http://www.imsglobal.org/xsd/apip/apipv1p0/imsapip_qtiv1p0"/>
</Item>

回答1:


You could use modify and delete:

UPDATE tab
SET col.modify('
  declare namespace NS="http://www.imsglobal.org/xsd/imsqti_v2p2";
  declare namespace NS2="http://www.imsglobal.org/xsd/apip/apipv1p0/imsapip_qtiv1p0";
  delete /NS:Item/NS2:apipAccessibility/*')
-- WHERE id = 1;

LiveDemo

Output:

<Item xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2" title="1234">
  <ItemBody>xyz</ItemBody>
  <apipAccessibility xmlns="http://www.imsglobal.org/xsd/apip/apipv1p0/imsapip_qtiv1p0" />
</Item> 


来源:https://stackoverflow.com/questions/35873792/replace-the-xml-element-with-new-element-in-sql-server

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