Replace the XML element with new Element in SQL se

2019-06-05 19:09发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-05 19:32

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> 
查看更多
登录 后发表回答