I have xml data that looks like this:
<game>
<teams>
<home id="363" color="000099">Brazil</home>
<away id="375" color="c5b358">Germany</away>
</teams>
<gameInfo>
<homeScore>1</homeScore>
<awayScore>7</awayScore>
<clock>90</clock>
</gameInfo>
</game>
I would like to create a table that has the columns in this order: home,away,homeID, awayID, homeScore, awayScore. I can't seem to get the home id (363 and 375) into the table:
select *
from (
select
e.value('(./teams/home/text())[1]', 'nvarchar(100)') home,
e.value('(./teams/away/text())[1]', 'nvarchar(100)') away,
e.value('./teams/home/@id', 'int') homeID,
e.value('./teams/away/@id', 'int') awayID
e.value('(./gameInfo/homeScore/text())[1]', 'int') homeScore,
e.value('(./gameInfo/awayScore/text())[1]', 'int') awayScore
from (select * from [XMLTest].[dbo].[MATCHES]) t
cross apply t.data.nodes('game') as t2(e)
) events