使用 FOR XML 控制 XML 元素的嵌套

Control on XML elements nesting using FOR XML(使用 FOR XML 控制 XML 元素的嵌套)
本文介绍了使用 FOR XML 控制 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)

这是期望的结果(员工、部门和安全是不同的元素 - 我的问题是像在这个示例中一样创建员工部门和安全):

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 元素的嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)