从SQL中的所有XML节点中删除属性

时间:2018-02-22 07:36:52

标签: sql-server xml-parsing xquery-sql

如何通过T-sql从XML的所有节点中删除属性“CRS”。如果存在于XML中,则希望删除属性“CRS”。

   <list>
<group id="12345">
    <entry id="1" type="Audio" lang="en-us">
        <p data-its-style="">
            <audio xmlns="http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0" class="sound_explicit">
                <source crs="test1_en.ogg" type="audio/ogg; " src="1234.ogg" />
                <source crs="test1_en.m4a" type="audio/mp4;" src="4567.mp4" />
            </audio>
        </p>
    </entry>
</group>
<group id="67890">
    <entry id="4" type="Audio" lang="es-mx">
            <p data-its-style="">
            <audio xmlns="http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0"  class="sound_explicit">
                <source crs="test4_en.ogg" type="audio/ogg; " src="1234.ogg" />
                <source crs="test4_en.m4a" type="audio/mp4;" src="4567.mp4" />
            </audio>
        </p>
    </entry>
</group>

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题

    DECLARE @xml XML = '<list>
    <group id="12345">
        <entry id="1" type="Audio" lang="en-us">
            <p data-its-style="">
                <audio xmlns="http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0" class="sound_explicit">
                    <source crs="test1_en.ogg" type="audio/ogg; " src="1234.ogg" />
                    <source crs="test1_en.m4a" type="audio/mp4;" src="4567.mp4" />
                </audio>
            </p>
        </entry>
    </group>
    <group id="67890">
        <entry id="4" type="Audio" lang="es-mx">
                <p data-its-style="">
                <audio xmlns="http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0"  class="sound_explicit">
                    <source crs="test4_en.ogg" type="audio/ogg; " src="1234.ogg" />
                    <source crs="test4_en.m4a" type="audio/mp4;" src="4567.mp4" />
                </audio>
            </p>
        </entry>
    </group>
    </list>'

    /*Before*/
    SELECT @xml
    /*Delete*/
    SET @xml.modify ('declare namespace ns="http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0";  delete list/group/entry/p/ns:audio/ns:source/@crs')
    /*After*/
    SELECT @xml