SQL Server将XML解析为键/值对

时间:2018-03-23 16:12:12

标签: sql-server xml parsing

我需要解析这个XML文件:

<SystemConfiguration Model="PowerEdge R740xd" ServiceTag="Test" TimeStamp="Wed Feb 14">
<Component FQDD="iDRAC.Embedded.1">
 <Attribute Name="Info.1#Product">Integrated Dell Remote Access Controller</Attribute>
 <Attribute Name="IPMILan.1#Enable">Disabled</Attribute>
 <Attribute Name="IPMILan.1#PrivLimit">Administrator</Attribute>
 <Attribute Name="IPMILan.1#EncryptionKey">0</Attribute>

 <!-- <Attribute Name="AutoBackup.1#IPAddress"></Attribute> -->
 <!-- <Attribute Name="AutoBackup.1#Domain"></Attribute> -->
</Component>
<Component FQDD="RAID.Integrated.1-1">
  <Attribute Name="RAIDresetConfig">True</Attribute>
    <Attribute Name="RAIDforeignConfig">Clear</Attribute>
    <Attribute Name="RAIDrekey">False</Attribute>
    <Attribute Name="EncryptionMode">None</Attribute>

    <Component FQDD="Disk.Virtual.8:RAID.Integrated.1-1">
        <Attribute Name="RAIDaction">Create</Attribute>
        <Attribute Name="LockStatus">Unlocked</Attribute>
        <Attribute Name="RAIDinitOperation">None</Attribute>
        <Attribute Name="DiskCachePolicy">Default</Attribute>
        <Attribute Name="RAIDdefaultWritePolicy">WriteBack</Attribute>
        <Attribute Name="RAIDdefaultReadPolicy">ReadAhead</Attribute>
        <Attribute Name="Name">Virtual Disk 8</Attribute>
        <Attribute Name="Size">0</Attribute>
        <Attribute Name="StripeSize">0</Attribute>
        <Attribute Name="SpanDepth">1</Attribute>
        <Attribute Name="SpanLength">1</Attribute>
        <Attribute Name="RAIDTypes">RAID 0</Attribute>
        <Attribute Name="IncludedPhysicalDiskID">Disk.Bay.7:Enclosure.Internal.0-1:RAID.Integrated.1-1</Attribute>
    </Component>
    <Component FQDD="Disk.Virtual.9:RAID.Integrated.1-1">
        <Attribute Name="RAIDaction">Create</Attribute>
        <Attribute Name="LockStatus">Unlocked</Attribute>
        <Attribute Name="RAIDinitOperation">None</Attribute>
        <Attribute Name="DiskCachePolicy">Default</Attribute>
        <Attribute Name="RAIDdefaultWritePolicy">WriteBack</Attribute>
        <Attribute Name="RAIDdefaultReadPolicy">ReadAhead</Attribute>
        <Attribute Name="Name">Virtual Disk 9</Attribute>
        <Attribute Name="Size">0</Attribute>
        <Attribute Name="StripeSize">0</Attribute>
        <Attribute Name="SpanDepth">1</Attribute>
        <Attribute Name="SpanLength">1</Attribute>
        <Attribute Name="RAIDTypes">RAID 0</Attribute>
        <Attribute Name="IncludedPhysicalDiskID">Disk.Bay.6:Enclosure.Internal.0-1:RAID.Integrated.1-1</Attribute>
    </Component>
    <Component FQDD="Enclosure.Internal.0-1:RAID.Integrated.1-1">
        <Component FQDD="Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1">
            <Attribute Name="RAIDHotSpareStatus">No</Attribute>
            <Attribute Name="RAIDPDState">Ready</Attribute>
        </Component>
        <Component FQDD="Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1">
            <Attribute Name="RAIDHotSpareStatus">No</Attribute>
            <Attribute Name="RAIDPDState">Ready</Attribute>
        </Component>
    </Component>
    <Attribute Name="RAIDremoveControllerKey">False</Attribute>
</Component>
</SystemConfiguration>

目标是将数据转换为以下列中的键值对:

  

模特| ServiceTag | TimeStamp |组件|属性|值

我可以用当前的格式做到吗?我知道有多个组件,我想要做的是连接任何需要它的组件。

此数据是使用SSIS从XML文件引入的。它将整个XML放在一个字符串中,然后使用触发器进行解析。

1 个答案:

答案 0 :(得分:1)

这不会反映组件之间的关系(如何嵌套),但它会返回您似乎需要的列表:

DECLARE @xml XML=N'<SystemConfiguration Model="PowerEdge R740xd" ServiceTag="Test" TimeStamp="Wed Feb 14">
<Component FQDD="iDRAC.Embedded.1">
 <Attribute Name="Info.1#Product">Integrated Dell Remote Access Controller</Attribute>
 <Attribute Name="IPMILan.1#Enable">Disabled</Attribute>
 <Attribute Name="IPMILan.1#PrivLimit">Administrator</Attribute>
 <Attribute Name="IPMILan.1#EncryptionKey">0</Attribute>

 <!-- <Attribute Name="AutoBackup.1#IPAddress"></Attribute> -->
 <!-- <Attribute Name="AutoBackup.1#Domain"></Attribute> -->
</Component>
<Component FQDD="RAID.Integrated.1-1">
  <Attribute Name="RAIDresetConfig">True</Attribute>
    <Attribute Name="RAIDforeignConfig">Clear</Attribute>
    <Attribute Name="RAIDrekey">False</Attribute>
    <Attribute Name="EncryptionMode">None</Attribute>

    <Component FQDD="Disk.Virtual.8:RAID.Integrated.1-1">
        <Attribute Name="RAIDaction">Create</Attribute>
        <Attribute Name="LockStatus">Unlocked</Attribute>
        <Attribute Name="RAIDinitOperation">None</Attribute>
        <Attribute Name="DiskCachePolicy">Default</Attribute>
        <Attribute Name="RAIDdefaultWritePolicy">WriteBack</Attribute>
        <Attribute Name="RAIDdefaultReadPolicy">ReadAhead</Attribute>
        <Attribute Name="Name">Virtual Disk 8</Attribute>
        <Attribute Name="Size">0</Attribute>
        <Attribute Name="StripeSize">0</Attribute>
        <Attribute Name="SpanDepth">1</Attribute>
        <Attribute Name="SpanLength">1</Attribute>
        <Attribute Name="RAIDTypes">RAID 0</Attribute>
        <Attribute Name="IncludedPhysicalDiskID">Disk.Bay.7:Enclosure.Internal.0-1:RAID.Integrated.1-1</Attribute>
    </Component>
    <Component FQDD="Disk.Virtual.9:RAID.Integrated.1-1">
        <Attribute Name="RAIDaction">Create</Attribute>
        <Attribute Name="LockStatus">Unlocked</Attribute>
        <Attribute Name="RAIDinitOperation">None</Attribute>
        <Attribute Name="DiskCachePolicy">Default</Attribute>
        <Attribute Name="RAIDdefaultWritePolicy">WriteBack</Attribute>
        <Attribute Name="RAIDdefaultReadPolicy">ReadAhead</Attribute>
        <Attribute Name="Name">Virtual Disk 9</Attribute>
        <Attribute Name="Size">0</Attribute>
        <Attribute Name="StripeSize">0</Attribute>
        <Attribute Name="SpanDepth">1</Attribute>
        <Attribute Name="SpanLength">1</Attribute>
        <Attribute Name="RAIDTypes">RAID 0</Attribute>
        <Attribute Name="IncludedPhysicalDiskID">Disk.Bay.6:Enclosure.Internal.0-1:RAID.Integrated.1-1</Attribute>
    </Component>
    <Component FQDD="Enclosure.Internal.0-1:RAID.Integrated.1-1">
        <Component FQDD="Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1">
            <Attribute Name="RAIDHotSpareStatus">No</Attribute>
            <Attribute Name="RAIDPDState">Ready</Attribute>
        </Component>
        <Component FQDD="Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1">
            <Attribute Name="RAIDHotSpareStatus">No</Attribute>
            <Attribute Name="RAIDPDState">Ready</Attribute>
        </Component>
    </Component>
    <Attribute Name="RAIDremoveControllerKey">False</Attribute>
</Component>
</SystemConfiguration>';

- 查询直接从@xml获取元数据,并使用深度搜索//将所有<Attribute>个节点作为派生表获取。每个<Attribute>都位于<Component>的正下方,您可以使用后跳..来实现目标:

SELECT @xml.value('(SystemConfiguration/@Model)[1]','nvarchar(max)') AS Model
      ,@xml.value('(SystemConfiguration/@ServiceTag)[1]','nvarchar(max)') AS ServiceTag
      ,@xml.value('(SystemConfiguration/@TimeStamp)[1]','nvarchar(max)') AS TimeStamp
      ,a.value('(../@FQDD)[1]','nvarchar(max)') AS Component_FQDD
      ,a.value('@Name','nvarchar(max)') AS Attribute
      ,a.value('text()[1]','nvarchar(max)') AS Value
FROM @xml.nodes('//Attribute') AS A(a);

结果(某些行)

PowerEdge R740xd    Test    Wed Feb 14  iDRAC.Embedded.1    Info.1#Product  Integrated Dell Remote Access Controller
PowerEdge R740xd    Test    Wed Feb 14  iDRAC.Embedded.1    IPMILan.1#Enable    Disabled
PowerEdge R740xd    Test    Wed Feb 14  iDRAC.Embedded.1    IPMILan.1#PrivLimit    Administrator