XML Parsing Help

  • We receive a full block of XML with various segments on it.

    Need help in separating XML segments into separate XML chunks into local xml variables.

    The local xml variable, holding the separated xml segment, will be passed on parameter

    to another Store Procedure.

    For eg:

    Declare @Message xml

    set @Message = '<Message>

    <Procedure>sp_testProc</Procedure>

    <Customer>

    <row>

    <CustID>111</CustID>

    <CustName>TestName2</CustName>

    </row>

    <row>

    <CustID>222</CustID>

    <CustName>TestName2</CustName>

    </row>

    </Customer>

    <Product>

    <ProdCode>AA</ProdCode>

    <ProdName>TestProdAA</ProdName>

    </Product>

    </Message>'

    select @Message

    Declare @Proc xml

    Declare @Customer XML

    Declare @product xml

    ----Need query help to extract as below, from @Message.

    set @Proc = '<Procedure>sp_testProc</Procedure>'

    set @Customer = '<Customer>

    <row>

    <CustID>111</CustID>

    <CustName>TestName2</CustName>

    </row>

    <row>

    <CustID>222</CustID>

    <CustName>TestName2</CustName>

    </row>

    </Customer>'

    set @product =

    '<Product>

    <ProdCode>AA</ProdCode>

    <ProdName>TestProdAA</ProdName>

    </Product>'

    Appreciate any help on this, and need this to work on both SQL 2012 and 2005.

  • Quick suggestion

    😎

    Declare @Message xml

    Declare @Proc xml

    Declare @Customer XML

    Declare @product xml

    set @Message = '<Message>

    <Procedure>sp_testProc</Procedure>

    <Customer>

    <row>

    <CustID>111</CustID>

    <CustName>TestName2</CustName>

    </row>

    <row>

    <CustID>222</CustID>

    <CustName>TestName2</CustName>

    </row>

    </Customer>

    <Product>

    <ProdCode>AA</ProdCode>

    <ProdName>TestProdAA</ProdName>

    </Product>

    </Message>'

    select

    @Proc = @Message.query('Message/Procedure')

    ,@Customer= @Message.query('Message/Customer')

    ,@Product = @Message.query('Message/Product')

    ;

    Output

    @Proc

    <Procedure>sp_testProc</Procedure>

    @Customer

    <Customer><row><CustID>111</CustID><CustName>TestName2</CustName></row><row><CustID>222</CustID><CustName>TestName2</CustName></row></Customer>

    @product

    <Product><ProdCode>AA</ProdCode><ProdName>TestProdAA</ProdName></Product>

  • Thank you. The solution worked.

  • s-sql (6/28/2016)


    Thank you. The solution worked.

    You are very welcome.

    😎

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply