我想在父节点为<val>
标记的每个<interv>
标记中获取值。如果您看到以下示例。我在<val>
代码48和1500中有两个值。
我的查询返回481500.我正在显示一个示例xml和查询,我在下面尝试过。在其他一些xml中,我可能有10个以上的值 - 能不能帮助我吗?
示例XML:
declare @xml xml =
'<maintask chg="U" identask="25/63/00/000/000/056" nrd="-" revdate="2013.12.05" version="B2,B3">
<title chg="N">Electric hoist 136 kg</title>
<mainlimi category="1">
<limit msmgeographic="" type="TBO" />
<nextperf>
<interv>
<msmlimitorder>1</msmlimitorder>
<msmtemporarylimit>False</msmtemporarylimit>
<val>48</val>
<unit>M</unit>
<margin>
<val>146</val>
<unit>D</unit>
</margin>
<marginmin>
<val>0</val>
<unit>D</unit>
</marginmin>
<marginmax>
<val>0</val>
<unit>D</unit>
</marginmax>
</interv>
<interv>
<msmlimitorder>2</msmlimitorder>
<msmlimitoperator>//</msmlimitoperator>
<msmtemporarylimit>False</msmtemporarylimit>
<val>1500</val>
<unit>HC</unit>
<margin>
<val>150</val>
<unit>HC</unit>
</margin>
<marginmin>
<val>0</val>
<unit>HC</unit>
</marginmin>
<marginmax>
<val>0</val>
<unit>HC</unit>
</marginmax>
</interv>
</nextperf>
<pnrlist>
<pnrdata>
<pnr wp6sourceid="4">76370-110</pnr>
<validity />
<pn>704A41815032</pn>
</pnrdata>
<pnrdata>
<pnr wp6sourceid="5">76370-111</pnr>
<validity />
<pn>704A41815033</pn>
</pnrdata>
<pnrdata>
<pnr>xna</pnr>
<validity />
<pn>704A41815035</pn>
</pnrdata>
<pnrdata>
<pnr wp6sourceid="7">76370-130-D</pnr>
<validity />
<pn>-</pn>
</pnrdata>
<pnrdata>
<pnr wp6sourceid="8">76370-140-D</pnr>
<validity />
<pn>704A41815085</pn>
</pnrdata>
</pnrlist>
</mainlimi>
<desc>
<para chg="N">LUCAS AIR EQUIPEMENT/TRW/GOODRICH.</para>
</desc>
<nrd code="-">
<para chg="N" />
</nrd>
<climaticconditionlist>
<climaticcondition code="-">
<para chg="N" />
</climaticcondition>
</climaticconditionlist>
</maintask>'
我的查询:
SELECT
ISNULL(TM.value('@identask', 'VARCHAR(100)'), '') AS MI,
TM.query('title').value('.', 'NVARCHAR(100)') AS Title,
TM.query('desc/para').value('.', 'NVARCHAR(100)') AS Remarks,
TM.query('mainlimi/nextperf/interv/val').value('.', 'NVARCHAR(100)') AS val,
TM.query('mainlimi/nextperf/interv/unit').value('.','NVARCHAR(100)') AS unit
FROM
@xml.nodes('/maintask') AS TM(TM)
结果:
MI Title Remarks val unit
25/63/00/000/000/056 Electric hoist 136 kg LUCAS AIR EQUIPEMENT/TRW/GOODRICH. 481500 MHC
预期结果:
MI Title Remarks val unit
25/63/00/000/000/056 Electric hoist 136 kg LUCAS AIR EQUIPEMENT/TRW/GOODRICH. 48 M
25/63/00/000/000/056 Electric hoist 136 kg LUCAS AIR EQUIPEMENT/TRW/GOODRICH. 1500 HC
答案 0 :(得分:3)
你差不多完成了。只需使用CROSS APPLY
来查看查询值所需的项目:
SELECT ISNULL(P.c.value('@identask','VARCHAR(100)') ,'')AS MI
,P.c.query('title').value('.','NVARCHAR(100)') AS Title
,P.c.query('desc/para').value('.','NVARCHAR(100)') AS Remarks
,T.c.value('(./val)[1]', 'NVARCHAR(100)') AS val
,T.c.value('(./unit)[1]', 'NVARCHAR(100)') AS unit
FROM @xml.nodes('/maintask') P(c)
CROSS APPLY P.c.nodes('./mainlimi/nextperf/interv') T(c);