Help on XML parsing

  • How should I parse this XML so that the output includes the ServiceLineID?

    SET QUOTED_IDENTIFIER ON

    declare @xml xml

    set @xml = (pls see the attached file for the structure...)

    '

    '

    SELECT 1 AS ServiceLineNumber ,

    Node.value('@Name', 'varchar(50)') as FieldName,

    Node.query('New').value('.','varchar(100)') AS OldValue,

    Node.query('Old').value('.','varchar(100)') AS NewValue

    FROM @xml.nodes('/ServiceLine/Field') TempXML(Node)

    RESULT:

    **********

    FieldNameOldValueNewValue

    ========================================================

    OrderDate1/1/20082/2/2008

    Messagestestt

    ShipNameHomeOffice

    ShipCityNYSF

    IDeAL RESULT:

    *************

    ServiceLineIDFieldNameOldValueNewValue

    ========================================================

    1OrderDate1/1/20082/2/2008

    1Messagestestt

    2ShipNameHome Office

    2

  • Iany,

    This editor does not allow XML tags and hence your XML data does not show up in the post. Please replace XML tags ('<>') with '[]' and repost.

    .

  • SELECTNode.value('../@ID', 'int') AS ServiceLineNumber,

    Node.value('@Name', 'varchar(50)') as FieldName,

    Node.query('New').value('.','varchar(100)') AS OldValue,

    Node.query('Old').value('.','varchar(100)') AS NewValue

    FROM@xml.nodes('/ServiceLine/Field') TempXML(Node)


    N 56°04'39.16"
    E 12°55'05.25"

  • Ah, I did not notice the attachment!

    May be a little easier method using a CROSS APPLY.

    SELECT

    Node.value('@ID','INT') AS ServiceLineNumber,

    Node1.value('@Name', 'varchar(20)') as FieldName,

    Node1.query('New').value('.','varchar(20)') AS OldValue,

    Node1.query('Old').value('.','varchar(20)') AS NewValue

    FROM @xml.nodes('/ServiceLine') t(Node)

    CROSS APPLY Node.nodes('Field') t1(Node1)

    /*

    ServiceLineNumber FieldName OldValue NewValue

    ----------------- -------------------- -------------------- --------------------

    1 OrderDate 1/1/2008 2/2/2008

    1 Messages test t

    2 ShipName Home Office

    2 ShipCity NY SF

    */

    .

  • Thanks!

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

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