Specify the element name for the subquery using a column alias instead of root()
. That will make Value1
and Value2
end up in the same node as the SubValue
's.
MS SQL Server 2008 Schema Setup:
create table Entity_A
(
ID int,
Name varchar(10),
Description varchar(20),
Value1 varchar(10),
Value2 varchar(10)
);
create table Entity_B
(
ID int,
ID_Entity_A int,
SubValue char(3)
);
insert into Entity_A values
(10, 'aName1', 'aDescription1', 'AttVal1', 'AttVal2'),
(11, 'aName2', 'aDescription2', 'AttVal21', 'AttVal25');
insert into Entity_B values
(1, 10, 'sv1'),
(2, 10, 'sv2'),
(3, 10, 'sv3'),
(4, 10, 'sv4'),
(5, 11, 'sv5'),
(6, 11, 'sv6');
Query 1:
select A.Name as [Identification/Name],
A.Description as [Identification/Description],
A.Value1 as [EntityAttribute/Value1],
A.Value2 as [EntityAttribute/Value2],
(
select B.SubValue
from dbo.Entity_B as B
where A.ID = B.ID_Entity_A
for xml path(''), type
) as [EntityAttribute]
from dbo.Entity_A as A
for xml path('EntityNode'), root('MyRoot');
<MyRoot>
<EntityNode>
<Identification>
<Name>aName1</Name>
<Description>aDescription1</Description>
</Identification>
<EntityAttribute>
<Value1>AttVal1</Value1>
<Value2>AttVal2</Value2>
<SubValue>sv1</SubValue>
<SubValue>sv2</SubValue>
<SubValue>sv3</SubValue>
<SubValue>sv4</SubValue>
</EntityAttribute>
</EntityNode>
<EntityNode>
<Identification>
<Name>aName2</Name>
<Description>aDescription2</Description>
</Identification>
<EntityAttribute>
<Value1>AttVal21</Value1>
<Value2>AttVal25</Value2>
<SubValue>sv5</SubValue>
<SubValue>sv6</SubValue>
</EntityAttribute>
</EntityNode>
</MyRoot> |