【问题标题】:Remove attribute from all nodes of XML in SQL从 SQL 中的 XML 的所有节点中删除属性
【发布时间】:2023-03-23 21:46:01
【问题描述】:

如何通过 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>

【问题讨论】:

  • 见下面的解决方案

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


【解决方案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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2021-10-13
    • 2016-11-13
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    相关资源
    最近更新 更多