从SQL Server中的XML中提取信息

时间:2017-11-10 21:30:47

标签: sql sql-server xml tsql

我需要从SQL Server中的XML中提取信息。该文件的格式类似于下面的格式。我在解析这种文档样式的信息时遇到了问题。有谁知道为什么要做到这一点?

<ExtractSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConnectorSettings connectorScriptId="74" channelDomain="Diag" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
    <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="15" channelDomain="Doc" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
      <StepSettings stepScriptId="61" stepType="Lab" isActive="true" sequence="3" />
   </ConnectorSettings>
  <ConnectorSettings connectorScriptId="12" channelDomain="Imm" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="16" stepType="Imm" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="46" channelDomain="Lab" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
  </ConnectorSettings>
</ExtractSettings>

我之前在SQL Server中做过这个,但格式是这样的

  <ConnectorSetting>
     <StepSettings>70<StepSettings/>
        <Steptype>Enc<Steptype/>
     <StepSettings>23<StepSettings/>
        <Steptype>Demo<Steptype/>
  </ConnectorSettings>

这使得处理起来更加容易。

我尝试过几件事,但主要是试图让这些工作

 select 
     stuff.connectorSettings.value('stepScriptID', 'varchar(50)') 
 from 
     @xml.nodes('ROOT/ConnectorSettings/stepscript') as  stuff(connectorSettings);

exec sp_xml_preparedocument @idoc output, @xml;
select * from OPENXML(@idoc,'/ROOT/ConnectorSettings',4)

我现在有点失落所以任何建议都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

以下是一些可以帮助您入门的示例:

declare @doc xml = '<ExtractSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConnectorSettings connectorScriptId="74" channelDomain="Diag" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
    <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="15" channelDomain="Doc" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="23" stepType="Enc" isActive="true" sequence="2" />
      <StepSettings stepScriptId="61" stepType="Lab" isActive="true" sequence="3" />
   </ConnectorSettings>
  <ConnectorSettings connectorScriptId="12" channelDomain="Imm" isActive="true">
      <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
      <StepSettings stepScriptId="16" stepType="Imm" isActive="true" sequence="2" />
  </ConnectorSettings>
  <ConnectorSettings connectorScriptId="46" channelDomain="Lab" isActive="true">
    <StepSettings stepScriptId="9" stepType="Demo" isActive="true" sequence="1" />
  </ConnectorSettings>
</ExtractSettings>';

select cs.value('@connectorScriptId','int') connectorScriptId, 
       ss.value('@stepType','varchar(20)') stepType
from @doc.nodes('/ExtractSettings/ConnectorSettings') c(cs)
outer apply cs.nodes('StepSettings') cs(ss)

输出

connectorScriptId stepType
----------------- --------------------
74                Demo
74                Enc
15                Demo
15                Enc
15                Lab
12                Demo
12                Imm
46                Demo

(8 rows affected)

答案 1 :(得分:0)

考虑到XML变量@xml中的XML,您可以尝试这样的事情:

SELECT
    ConnectorScriptId = xc.value('../@connectorScriptId', 'int'),
    StepScriptId = xc.value('@stepScriptId', 'int'),
    StepType = xc.value('@stepType', 'varchar(50)'),
    Sequence = xc.value('@sequence', 'int')
FROM
    @xml.nodes('/ExtractSettings/ConnectorSettings/StepSettings') AS XT(XC)

那应该给你这样的东西:

enter image description here