I have a XML data like this :
我有这样的XML数据:
<root>
<log realm="ABC" at="Wed Oct 15 00:00:02 2014.211" lifespan="2279ms">
<receive>
<isomsg direction="IN">
<header>6000911384</header>
<field id="0" value="0800"/>
<field id="3" value="980000"/>
<field id="11" value="000852"/>
</isomsg>
</receive>
</log>
</root>
how can I transform that XML data into table like this :
如何将XML数据转换成这样的表:
AT |lifespan|direction |ID |Value
---------------------------------------------
Wed Oct 15 2014|2279ms |in |0 |0800
Wed Oct 15 2014|2279ms |in |3 |980000
Wed Oct 15 2014|2279ms |in |11 |000852
2 个解决方案
#1
4
This would be a lot easier than @Nick's answer, since it only needs one .nodes()
call instead of three nested ones...
这比@Nick的答案要简单得多,因为它只需要一个.nodes()调用,而不是三个嵌套的……
DECLARE @input XML = '<root>
<log realm="ABC" at="Wed Oct 15 00:00:02 2014.211" lifespan="2279ms">
<receive>
<isomsg direction="IN">
<header>6000911384</header>
<field id="0" value="0800"/>
<field id="3" value="980000"/>
<field id="11" value="000852"/>
</isomsg>
</receive>
</log>
</root>'
SELECT
At = xc.value('../../../@at', 'varchar(50)'),
Lifespan = xc.value('../../../@lifespan', 'varchar(25)'),
Direction = xc.value('../@direction', 'varchar(10)'),
ID = XC.value('@id', 'int'),
Value = xc.value('@value', 'varchar(25)')
FROM
@Input.nodes('/root/log/receive/isomsg/field') AS XT(XC)
The call to @Input.nodes
basically returns a "virtual" table of XML fragments, representing each of the <field>
XML elements. By using the ..
we can also navigate "up the" XML hierarchy in the original document to access the <isomsg>
and <log>
elements and grab attribute values from those
@Input的电话。节点基本上返回一个“虚拟”XML片段表,表示每个
#2
3
Here is how I did it (although there are several ways).
我是这样做的(尽管有几种方法)。
WITH xmlData AS (
SELECT CAST('<root>
<log realm="ABC" at="Wed Oct 15 00:00:02 2014.211" lifespan="2279ms">
<receive>
<isomsg direction="IN">
<header>6000911384</header>
<field id="0" value="0800"/>
<field id="3" value="980000"/>
<field id="11" value="000852"/>
</isomsg>
</receive>
</log>
</root>' AS XML) as xmlData)
SELECT xmlData, logs.x.value('(@at)[1]','varchar(50)') as 'Element1', logs.x.value('(@lifespan)[1]','varchar(50)') as 'Element2', ismsgs.logs.value('(@direction)[1]','varchar(50)') as 'Element3', fields.ismsgs.value('(@value)[1]','varchar(50)') as 'Element4'
FROM xmlData x
CROSS APPLY xmlData.nodes('root/log') logs(x)
CROSS APPLY xmlData.nodes('root/log/receive/isomsg') ismsgs(logs)
CROSS APPLY xmlData.nodes('root/log/receive/isomsg/field') fields(ismsgs)
#1
4
This would be a lot easier than @Nick's answer, since it only needs one .nodes()
call instead of three nested ones...
这比@Nick的答案要简单得多,因为它只需要一个.nodes()调用,而不是三个嵌套的……
DECLARE @input XML = '<root>
<log realm="ABC" at="Wed Oct 15 00:00:02 2014.211" lifespan="2279ms">
<receive>
<isomsg direction="IN">
<header>6000911384</header>
<field id="0" value="0800"/>
<field id="3" value="980000"/>
<field id="11" value="000852"/>
</isomsg>
</receive>
</log>
</root>'
SELECT
At = xc.value('../../../@at', 'varchar(50)'),
Lifespan = xc.value('../../../@lifespan', 'varchar(25)'),
Direction = xc.value('../@direction', 'varchar(10)'),
ID = XC.value('@id', 'int'),
Value = xc.value('@value', 'varchar(25)')
FROM
@Input.nodes('/root/log/receive/isomsg/field') AS XT(XC)
The call to @Input.nodes
basically returns a "virtual" table of XML fragments, representing each of the <field>
XML elements. By using the ..
we can also navigate "up the" XML hierarchy in the original document to access the <isomsg>
and <log>
elements and grab attribute values from those
@Input的电话。节点基本上返回一个“虚拟”XML片段表,表示每个
#2
3
Here is how I did it (although there are several ways).
我是这样做的(尽管有几种方法)。
WITH xmlData AS (
SELECT CAST('<root>
<log realm="ABC" at="Wed Oct 15 00:00:02 2014.211" lifespan="2279ms">
<receive>
<isomsg direction="IN">
<header>6000911384</header>
<field id="0" value="0800"/>
<field id="3" value="980000"/>
<field id="11" value="000852"/>
</isomsg>
</receive>
</log>
</root>' AS XML) as xmlData)
SELECT xmlData, logs.x.value('(@at)[1]','varchar(50)') as 'Element1', logs.x.value('(@lifespan)[1]','varchar(50)') as 'Element2', ismsgs.logs.value('(@direction)[1]','varchar(50)') as 'Element3', fields.ismsgs.value('(@value)[1]','varchar(50)') as 'Element4'
FROM xmlData x
CROSS APPLY xmlData.nodes('root/log') logs(x)
CROSS APPLY xmlData.nodes('root/log/receive/isomsg') ismsgs(logs)
CROSS APPLY xmlData.nodes('root/log/receive/isomsg/field') fields(ismsgs)