Home Forums SQL Server 2019 SQL Server 2019 - Development DBA Challenge! I've spent 4 hours trying to figure how to extract data from XML in a hierarchical form. RE: DBA Challenge! I've spent 4 hours trying to figure how to extract data from XML in a hierarchical form.

  • I cannot identify what you are looking for, but perhaps this will start you off along the right track.


    DECLARE @data XML = N'<navigation>
    <content label="Introduction">
    <content label="Intro" id="000286_1001"/>
    </content>
    <content label="Premature Atrial Contractions">
    <content label="Description" id="000286_1002"/>
    <content label="ECG Features" id="000286_1003"/>
    <content label="Etiology/Symptoms/Treatment" id="000286_1004"/>
    </content>
    <content label="Wandering">
    <content label="Description" id="000286_1005"/>
    <content label="ECG Features" id="000286_1006"/>
    <content label="Etiology/Symptoms/Treatment" id="000286_1007"/>
    </content>
    <content label="Ectopic Atrial Rhythm">
    <content label="Description" id="000286_1008"/>
    <content label="ECG Features" id="000286_1009"/>
    <content label="Etiology/Symptoms/Treatment" id="000286_1010"/>
    <content label="Supraventricular Tachycardia">
      <content label="SVT" id="000286_1011"/>
      <content label="Etiology/Symptoms/Treatment" id="000286_1012"/>
    </content>
    <content label="Multifocal Atrial Tachycardia">
      <content label="Description" id="000286_1013"/>
      <content label="ECG Features" id="000286_1014"/>
      <content label="Etiology/Symptoms/Treatment" id="000286_1015"/>
    </content>
    <content label="Ectopic Atrial Tachycardia">
      <content label="Description" id="000286_1016"/>
      <content label="ECG Features" id="000286_1017"/>
      <content label="Etiology/Symptoms/Treatment" id="000286_1018"/>
    </content>
    <content label="Ectopic Atrial Tachycardia with Block">
      <content label="Description" id="000286_1019"/>
    </content>
    <content label="Atrial Flutter">
      <content label="Description" id="000286_1020"/>
      <content label="ECG Features" id="000286_1021"/>
      <content label="Etiology/Symptoms/Treatment" id="000286_1022"/>
      <content label="Treatment" id="000286_1023"/>
    </content>
    <content label="Atrial Fibrillation">
      <content label="Description" id="000286_1024"/>
      <content label="ECG Features" id="000286_1025"/>
      <content label="Etiology/Symptoms/Treatment" id="000286_1026"/>
      <content label="Signs" id="000286_1027"/>
      <content label="Treatment" id="000286_1028"/>
    </content>
    <content label="AVRT">
      <content label="Description" id="000286_1029"/>
      <content label="Pathophysiology" id="000286_1030"/>
      <content label="ECG Features" id="000286_1031"/>
      <content label="Etiology" id="000286_1032"/>
    </content>
    <content label="AVNRT">
      <content label="Description" id="000286_1033"/>
      <content label="ECG Features" id="000286_1034"/>
      <content label="Etiology" id="000286_1035"/>
    </content>
    <content label="Treatment" id="000286_1036"/>
    </content>
    <content label="Review">
    <content label="Quick Quiz" id="000286_1037"/>
    </content>
    <content label="Conclusion">
    <content label="Summary" id="000286_1038"/>
    </content>
    </navigation> ';

    SELECT
      NavigationId = ROW_NUMBER() OVER (ORDER BY ISNULL(C.c.value('@id', 'VARCHAR(50)'), '999999_999')
                   , ISNULL(G.r.value('@id', 'VARCHAR(50)'), '999999_999'))
    , [Type] = T.l.value('@label', 'VARCHAR(100)')
    , Parent_label = C.c.value('@label', 'VARCHAR(100)')
    , Parent_id = C.c.value('@id', 'VARCHAR(50)')
    , Child_label = G.r.value('@label', 'VARCHAR(100)')
    , Child_id = G.r.value('@id', 'VARCHAR(50)')
    FROM @data.nodes('navigation/content') AS T(l)
    CROSS APPLY T.l.nodes('content') AS C(c)
    OUTER APPLY C.c.nodes('content') AS G(r);