Remove XML Nodes for matching attribute condition in SQLServer

Viewed 1222

I want to remove all the nodes in XML datatype column; matching the attribute condition for all the records.For example <resource> node which has attribute "type" whose value is like '%audio%'.

Input:

<metadata Item="1234" version="5" packageVersion="5" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">
  <contents>
    <content version="0" href="content_es-mx_en-us.xml" lang="es-mx" altlang="en-us">
      <resources />
    </content>
    <content version="3" href="content_en-us.xml" lang="en-us">
      <resources>
        <resource type="image/svg" href="girlwateringtomatoes2.svg" />
        <resource type="audio/ogg" href="Audio/115988A_Sound1.ogg" />
        <resource type="audio/mp4" href="Audio/115988A_Sound1.m4a" />  
      </resources>
    </content>
  </contents>
  <references />
  <brailleEncodings />
</metadata>

Output:

<metadata Item="1234" version="5" packageVersion="5" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">
  <contents>
    <content version="0" href="content_es-mx_en-us.xml" lang="es-mx" altlang="en-us">
      <resources />
    </content>
    <content version="3" href="content_en-us.xml" lang="en-us">
      <resources>
        <resource type="image/svg" href="girlwateringtomatoes2.svg" /> 
      </resources>
    </content>
  </contents>
  <references />
  <brailleEncodings />
</metadata>

Here is the query which i have tried but didn't work.

;WITH XMLNAMESPACES ('http://www.imsglobal.org/xsd/imsqti_v2p2' AS NS)
Update dbo.TableX 
SET [XML].modify('delete /NS:metadata/NS:contents/NS:content[2]/resources/resource[@type != "image/svg"]')

Thanks!

2 Answers
Related