我使用Nodes方法将一些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 Standard UOM Min Normal Max BeforeReading AfterReading DateCalibrated
我想看到的内容:
WOId CalibrationId Standard UOM Min Normal Max BeforeReading AfterReading
其中每个返回行的CalibrationId(使用上面的XML)3。
答案 0 :(得分: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)