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

  • Sometimes XML questions get no love around here.

    I am not great with the XML modify method (something I need to read up on and practice I guess) but here's an approach: we can cast the XML as a string and STUFF your row where required as shown here:

    -- sample table containing your xml

    DECLARE @table TABLE(id int identity, xcol xml);

    INSERT @table(xcol) VALUES(

    '<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>

    </row>');

    --Solution

    SELECT id,

    xcol =

    CAST(

    REPLACE(REPLACE(STUFF(

    CAST(xcol as varchar(8000)),

    CHARINDEX('<c4>',CAST(xcol as varchar(8000)))-1,0,'<ctest>New Added</ctest>'),'&#x0A',''),'>;<','><')

    AS xml)

    FROM @table;

    Another approach would be to use mdq.xmltransform if you are familiar with XSLT.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001