使用 FOR XML 控制嵌套的 XML 元素 [英] Control on XML elements nesting using FOR XML
问题描述
我举例说明我的问题.事实上,我在尝试了多种方法后找到了解决方案,但我想问一下这个解决方案是否好,或者是否出于任何原因最好使用替代方法.事实上,我需要控制元素的创建方式.
I state my problem by example. In fact i foudna solution after trying in many ways but i would like to ask whether this solution is good or if for any reason it is better to use an alternative approach. In fact i need to control how elements are created.
我首先创建了一个包含我需要的所有数据的视图,然后我通过多次加入视图从视图中选择.
i first made a view containing all the data i needed and then i selected from teh view by joining the view more times.
我在这里使用局部变量而不是视图重现了复杂性":
I reproduced the "complexity" here using a local variable instead of a view:
DECLARE @Employees table(
EmpID int NOT NULL,
Name nvarchar(50),
Surname nvarchar(50),
DateOfBirth date,
DepartmentID int,
AccessLevel int);
insert into @Employees values ('1', 'John','Doe','1980-01-31',100,5)
insert into @Employees values ('2', 'Mary','Rose','1971-02-27',102,3)
insert into @Employees values ('3', 'Luke','Perry','1995-12-01',104,1)
这是想要的结果(employee、department 和 security 是不同的元素 - 我的问题是像在这个例子中那样创建员工部门和 security):
This is the desired result (employee, department and security are differeent elements - my problem was to create employee department and security just like in this example):
<employee Name="John" Surname="Doe" DateOfBirth="1980-01-31">
<department DepartmentID="100">
<security AccessLevel="5" />
</department>
</employee>
<employee Name="Mary" Surname="Rose" DateOfBirth="1971-02-27">
<department DepartmentID="102">
<security AccessLevel="3" />
</department>
</employee>
<employee Name="Luke" Surname="Perry" DateOfBirth="1995-12-01">
<department DepartmentID="104">
<security AccessLevel="1" />
</department>
</employee>
正如我所说,我发现每个 xml 元素一次加入视图(这里是表变量)是一种解决方案:
As i said i found out that joining the view (here the table variable) one time per xml element is a solution:
-- declare @Employees table as above and then:
select
employee.Name,
employee.Surname,
employee.DateOfBirth,
department.DepartmentID,
security.AccessLevel from @Employees employee
join @Employees department on department.DepartmentID = employee.DepartmentID
join @Employees security on security.AccessLevel = employee.AccessLevel
for xml auto
这会产生所需的输出.
这种使用 for xml auto
进行多重连接的技术是否有效?
Is this techniwue of multiple joins with for xml auto
valid or not?
推荐答案
在别名中使用 @
在 xml
中生成 attributes
.更简单的方法来做到这一点
Use @
in alias names to generate attributes
in xml
. Much simpler way to do this
SELECT NAME AS [@Name],
Surname AS [@Surname],
DateOfBirth AS [@DateOfBirth],
DepartmentID AS [department/@DepartmentID],
AccessLevel AS [department/security/@AccessLevel]
FROM @Employees
FOR xml path('employee')
结果:
<employee Name="John" Surname="Doe" DateOfBirth="1980-01-31">
<department DepartmentID="100">
<security AccessLevel="5" />
</department>
</employee>
<employee Name="Mary" Surname="Rose" DateOfBirth="1971-02-27">
<department DepartmentID="102">
<security AccessLevel="3" />
</department>
</employee>
<employee Name="Luke" Surname="Perry" DateOfBirth="1995-12-01">
<department DepartmentID="104">
<security AccessLevel="1" />
</department>
</employee>
这篇关于使用 FOR XML 控制嵌套的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!