2017-11-10 118 views
1

我需要從SQL Server中的XML中提取信息。該文件的格式與下面的格式類似。我無法解析出這種文檔樣式的信息。有誰知道爲什麼要做到這一點?在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) 

我有點失去了在這一點所以任何的建議是,讚賞。

回答

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

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)