2017-04-17 70 views
1

我正在使用Nodes方法將一些XML數據分解到表中,而我只是缺少一個部分。我需要一個特定的XML元素,但是它的備份(原諒我糟糕的語法,處理XML的新問題)來自我正在粉碎的XML。我已經張貼,我希望是所有相關代碼:使用節點移動備份XML

XML:

<ToDoList xmlns="----.----.MappingObjects/XmlSchemas/ToDoList.xsd"> 
    <Step> 
    <StepId>f3ab5132-fcf4-47c4-929f-ab7464d3bcc7</StepId> 
    <Rank>1</Rank> 
    <IsComplete>true</IsComplete> 
    <CompletionDate>2017-04-17T17:12:45Z</CompletionDate> 
    <CompletionUser>Michael Johnson</CompletionUser> 
    <Text></Text> 
    <CalibrationId>3</CalibrationId> 
    <CalibrationValues> 
     <Value> 
     <Standard>1111</Standard> 
     <UnitOfMeasureName>NewU</UnitOfMeasureName> 
     <Min>1111</Min> 
     <Normal>1112</Normal> 
     <Max>1113</Max> 
     <BeforeReading>1111</BeforeReading> 
     <AfterReading>1112</AfterReading> 
     <DateCalibrated>2017-04-17T17:12:00Z</DateCalibrated> 
     <FailBefore>false</FailBefore> 
     <FailAfter>false</FailAfter> 
     </Value> 
     <Value> 
     <Standard>245.000</Standard> 
     <UnitOfMeasureName>NewU</UnitOfMeasureName> 
     <Min>245</Min> 
     <Normal>250</Normal> 
     <Max>280</Max> 
     <BeforeReading>245</BeforeReading> 
     <AfterReading>250</AfterReading> 
     <DateCalibrated>2017-04-17T17:12:00Z</DateCalibrated> 
     <FailBefore>false</FailBefore> 
     <FailAfter>false</FailAfter> 
     </Value> 
    </CalibrationValues> 
    </Step> 
</ToDoList> 

SQL:

;with XMLNAMESPACES('----.-----.MappingObjects/XmlSchemas/ToDoList.xsd' as NS) 

select WOId, 
     N.Step.value('(NS:Standard)[1]', 'nvarchar(max)') as [Standard], 
     N.Step.value('(NS:UnitOfMeasureName)[1]','nvarchar(32)') as [UOM], 
     N.Step.value('(NS:Min)[1]','float') as [Min], 
     N.Step.value('(NS:Normal)[1]','float') as Normal, 
     N.Step.value('(NS:Max)[1]','float') as [Max], 
     N.Step.value('(NS:BeforeReading)[1]','float') as BeforeReading, 
     N.Step.value('(NS:AfterReading)[1]','float') as AfterReading, 
     dbo.fn_clr_GetCustomerLocalTimeFromUTC(@CUSTOMERTIMEZONE, N.Step.value('(NS:DateCalibrated)[1]','datetime')) as DateCalibrated, 
     N.Step.value('(NS:FailBefore)[1]','bit') as FailBefore, 
     N.Step.value('(NS:FailAfter)[1]','bit') as FailAfter 


from tblWorkOrderToDoList WOTD 
cross apply Data.nodes('//NS:Value') N(Step) 

現有的表返回以下列(簡稱爲簡潔起見):

WOId標準計量單位最小值正常值最大值BeforeReading AfterReading DateCalibrated

我想看到的:

WOId CalibrationId標準計量單位最小值正常值最大值BeforeReading AfterReading

其中CalibrationId是(使用XML以上)3每個返回行。

回答

2

您可以撕碎的XML兩次,第一次在Step元的水平,然後在Value,這樣就可以從兩種元素得到必要的信息:

select 
    WOId, 
    T.Step.value('NS:CalibrationId[1]','int') as CalibrationId 
    N.Value.value('NS:Standard[1]', 'nvarchar(max)') as [Standard], 
    N.Value.value('NS:UnitOfMeasureName[1]','nvarchar(32)') as [UOM], 
    N.Value.value('NS:Min[1]','float') as [Min], 
    .... 
from tblWorkOrderToDoList WOTD 
cross apply WOTD.Data.nodes('//NS:Step') T(Step) 
cross apply T.Step.nodes('NS:CalibrationValues/NS:Value') N(Value) 
+0

或很可能不切碎兩次使用'..'移動up XML樹一級:'(../../ NS:CalibrationId)[1]' – har07