级别:硬 - 以层次结构格式从XML中提取数据

时间:2017-05-17 18:36:50

标签: sql-server xml tsql xml-parsing

我已经研究过,并没有想出任何人做这样的事情。我愿意接受建议。现在我正在尝试加载下面描述的表格。

    CREATE TABLE [Course].[Navigation](
        [NavigationId] [int] IDENTITY(1,1) NOT NULL,
        [ParentID] [int],
        [ChildID] [int], 
        [Type] [varchar](100) NULL,
        [Label] [int] NULL,
        [Id] [varchar](100) NULL )

代表课程的导航图。

我正在尝试加载的XML是:

        <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>
            <content label="Supraventricular Tachycardia">
                <content label="SVT" id="000286_1011"/>
                <content label="Etiology/Symptoms/Treatment" id="000286_1012"/>
                <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>

1 个答案:

答案 0 :(得分:3)

您可以使用递归查询执行此操作,更深入地处理嵌套的<content>

DECLARE @xml 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>
  <content label="Supraventricular Tachycardia">
    <content label="SVT" id="000286_1011" />
    <content label="Etiology/Symptoms/Treatment" id="000286_1012" />
    <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>';

查询将首先使用连接的序列(thx to John Cappelletti!)来获取原始订单,其次这可以用作id和parentId

WITH recCTE AS
(
    SELECT 1 AS Lvl
          ,nc.value(N'@label','nvarchar(max)') AS Label
          ,nc.value(N'@id','nvarchar(max)') AS id
          ,CAST(REPLACE(STR(ROW_NUMBER() OVER(ORDER BY (SELECT NULL)),3),' ','0') AS VARCHAR(MAX)) AS Seqnc
          ,CAST(NULL AS VARCHAR(MAX)) AS ParentSeqnc
          ,nc.query('./content') AS Deeper
    FROM @xml.nodes(N'/navigation/content') AS A(nc)

    UNION ALL

    SELECT recCTE.Lvl+1
          ,nc2.value(N'@label','nvarchar(max)') AS Label
          ,nc2.value(N'@id','nvarchar(max)') AS id
          ,recCTE.Seqnc+REPLACE(STR(ROW_NUMBER() OVER(ORDER BY (SELECT NULL)),3),' ','0')
          ,recCTE.Seqnc
          ,nc2.query('./content') AS Deeper
    FROM recCTE
    CROSS APPLY recCTE.Deeper.nodes(N'content') AS A(nc2)
)
SELECT Lvl
      ,id
      ,Seqnc
      ,ParentSeqnc
      ,Label  
FROM recCTE
ORDER BY Seqnc;

结果

+-----+-------------+-----------+-------------+---------------------------------------+
| Lvl | id          | Seqnc     | ParentSeqnc | Label                                 |
+-----+-------------+-----------+-------------+---------------------------------------+
| 1   | NULL        | 001       | NULL        | Introduction                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1001 | 001001    | 001         | Intro                                 |
+-----+-------------+-----------+-------------+---------------------------------------+
| 1   | NULL        | 002       | NULL        | Premature Atrial Contractions         |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1002 | 002001    | 002         | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1003 | 002002    | 002         | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1004 | 002003    | 002         | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 1   | NULL        | 003       | NULL        | Wandering                             |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1005 | 003001    | 003         | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1006 | 003002    | 003         | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1007 | 003003    | 003         | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 1   | NULL        | 004       | NULL        | Ectopic Atrial Rhythm                 |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1008 | 004001    | 004         | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1009 | 004002    | 004         | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1010 | 004003    | 004         | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 1   | NULL        | 005       | NULL        | Supraventricular Tachycardia          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1011 | 005001    | 005         | SVT                                   |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1012 | 005002    | 005         | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | NULL        | 005003    | 005         | Multifocal Atrial Tachycardia         |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1013 | 005003001 | 005003      | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1014 | 005003002 | 005003      | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1015 | 005003003 | 005003      | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | NULL        | 005004    | 005         | Ectopic Atrial Tachycardia            |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1016 | 005004001 | 005004      | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1017 | 005004002 | 005004      | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1018 | 005004003 | 005004      | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | NULL        | 005005    | 005         | Ectopic Atrial Tachycardia with Block |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1019 | 005005001 | 005005      | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | NULL        | 005006    | 005         | Atrial Flutter                        |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1020 | 005006001 | 005006      | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1021 | 005006002 | 005006      | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1022 | 005006003 | 005006      | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1023 | 005006004 | 005006      | Treatment                             |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | NULL        | 005007    | 005         | Atrial Fibrillation                   |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1024 | 005007001 | 005007      | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1025 | 005007002 | 005007      | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1026 | 005007003 | 005007      | Etiology/Symptoms/Treatment           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1027 | 005007004 | 005007      | Signs                                 |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1028 | 005007005 | 005007      | Treatment                             |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | NULL        | 005008    | 005         | AVRT                                  |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1029 | 005008001 | 005008      | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1030 | 005008002 | 005008      | Pathophysiology                       |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1031 | 005008003 | 005008      | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1032 | 005008004 | 005008      | Etiology                              |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | NULL        | 005009    | 005         | AVNRT                                 |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1033 | 005009001 | 005009      | Description                           |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1034 | 005009002 | 005009      | ECG Features                          |
+-----+-------------+-----------+-------------+---------------------------------------+
| 3   | 000286_1035 | 005009003 | 005009      | Etiology                              |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1036 | 005010    | 005         | Treatment                             |
+-----+-------------+-----------+-------------+---------------------------------------+
| 1   | NULL        | 006       | NULL        | Review                                |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1037 | 006001    | 006         | Quick Quiz                            |
+-----+-------------+-----------+-------------+---------------------------------------+
| 1   | NULL        | 007       | NULL        | Conclusion                            |
+-----+-------------+-----------+-------------+---------------------------------------+
| 2   | 000286_1038 | 007001    | 007         | Summary                               |
+-----+-------------+-----------+-------------+---------------------------------------+