Home Forums Programming XML Modifying XML with into or after RE: Modifying XML with into or after

  • Quick thought, only one modification can be done with modify at the time, therefore it may be simpler to use either a query or FLWOR (For, Let, Where, Order by, Return) for multiple inserts.

    😎

    Here is a simple example of multiple modifications using query

    DECLARE @OUTPUT XML;

    DECLARE @X XML = '

    <row id="1000000" xml:space="preserve">

    <c1>Exported</c1>

    <c2>Text Sample 1</c2>

    <c2 m="2">Text Sample 2</c2>

    <c2 m="3">Text Sample 3</c2>

    <c2 m="4">Text Sample 4</c2>

    <c3>Text Sample 1</c3>

    <c3 m="2">Text Sample 2</c3>

    <c3 m="3">Text Sample 3</c3>

    <c4>Text Sample</c4>

    <c5>Text Sample</c5>

    <t1>Text Sample</t1>

    </row>';

    SELECT @OUTPUT = @X.query('

    <row id="1000000" xml:space="preserve">

    <ctest>Added before the first element</ctest>

    { /row/c1 }

    <ctest>Added after c1 and before c2</ctest>

    { /row/c2 }

    <ctest>Added after c2 and before c3</ctest>

    { /row/c3 }

    <ctest>Added after c3 and before c4</ctest>

    { /row/c4 }

    <ctest>Added after c4 and before t1</ctest>

    { /row/t1 }

    <ctest>Added after the last element</ctest>

    </row>

    ');

    SELECT @OUTPUT;

    Ouput

    <row id="1000000" xml:space="preserve">

    <ctest>Added before the first element</ctest>

    <c1>Exported</c1>

    <ctest>Added after c1 and before c2</ctest>

    <c2>Text Sample 1</c2>

    <c2 m="2">Text Sample 2</c2>

    <c2 m="3">Text Sample 3</c2>

    <c2 m="4">Text Sample 4</c2>

    <ctest>Added after c2 and before c3</ctest>

    <c3>Text Sample 1</c3>

    <c3 m="2">Text Sample 2</c3>

    <c3 m="3">Text Sample 3</c3>

    <ctest>Added after c3 and before c4</ctest>

    <c4>Text Sample</c4>

    <ctest>Added after c4 and before t1</ctest>

    <t1>Text Sample</t1>

    <ctest>Added after the last element</ctest>

    </row>