使用带有层次结构的表的FOR XML PATH生成XML [英] Generate XML using FOR XML PATH from table with hierarchy

查看:62
本文介绍了使用带有层次结构的表的FOR XML PATH生成XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从像这样的表创建xml:

EL1 EL2 EL3 Attr01 Attr02 Attr03 Attr04

E10 ,,, a,b,c,d
E10,E1010 ,, a,b,c,d

E10,E1010,E101010,a,b,c,d

E10,E1010, E101020,a,b,c,d

E10,E1010,E101030,a,b,c,d

E10,E1020 ,, a,b,c,d

E10,E1020,E102010,a,b,c,d

E20 ,,, a,b,c,d

E20, E2010,,a,b,c,d

E20,E2010,E201010,a,b,c,d

E20,E2020 ,, a,b,c, d

E20,E2020,E202010,a,b,c,d

E20,E2020,E202020,a,b,c,d



层次结构是EL1 - EL2 - EL3,3列应该是xml的元素;

其他列为Attr01,Attr02,Attr03,Attr04应该是xml的属性;



实际表可能有超过500行(El1,EL2和EL3有很多值)。



预期的xml应该如下:

I need to create xml from a table like:
EL1 EL2 EL3 Attr01 Attr02 Attr03 Attr04
E10, , ,a,b,c,d
E10,E1010, ,a,b,c,d
E10,E1010,E101010,a,b,c,d
E10,E1010,E101020,a,b,c,d
E10,E1010,E101030,a,b,c,d
E10,E1020, ,a,b,c,d
E10,E1020,E102010,a,b,c,d
E20, , ,a,b,c,d
E20,E2010, ,a,b,c,d
E20,E2010,E201010,a,b,c,d
E20,E2020, ,a,b,c,d
E20,E2020,E202010,a,b,c,d
E20,E2020,E202020,a,b,c,d

The hierarchy is EL1--EL2--EL3, and the 3 columns should be elements of xml;
The other for columns Attr01,Attr02,Attr03,Attr04 should be attributes of xml;

The actual table could have more than 500 rows(there are many values for El1,EL2,and EL3).

The expected xml should like:

<root>
  <E10 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
    <E1010 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
      <E101010 Attr01="a" Attr02="b" Attr03="c" Attr04="d"/>
      <E101020 Attr01="a" Attr02="b" Attr03="c" Attr04="d"/>
      <E101030 Attr01="a" Attr02="b" Attr03="c" Attr04="d"/>
    </E1010>
    <E1020 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
      <E102010 Attr01="a" Attr02="b" Attr03="c" Attr04="d"/>
    </E1020>
  </E10>
  <E20 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
    <E2010 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
      <E201010 Attr01="a" Attr02="b" Attr03="c" Attr04="d"/>
    </E2010>
    <E2020 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
      <E202010 Attr01="a" Attr02="b" Attr03="c" Attr04="d"/>
      <E202020 Attr01="a" Attr02="b" Attr03="c" Attr04="d"/>
    </E2020>
  </E20>
</root>





我创建一个示例Src表:

CREATE TABLE Src



EL1 VARCHAR(10),

EL2 VARCHAR(10),

EL3 VARCHAR(10),

Attr01 VARCHAR(10),

Attr02 VARCHAR(10),

Attr03 VARCHAR(10),

Attr04 VARCHAR(10)



GO

INSERT INTO Src

(EL1 ,EL2,EL3,Attr01,Attr02,Attr03,Attr04



选择'E10','','','a','b','c ','d'

UNION SELECT'E10','E1010','','a','b','c','d'

UNION选择'E10','E1010','E101010','a','b','c','d'

UNION SELECT'E10','E1010','E101020', 'a','b','c','d'

UNION SELECT'E10','E1010','E101030','a','b','c',' d $

UNION SELECT'E10','E1020','','a','b','c','d'

UNION SELECT'E10 ','E1020','E102010','a','b','c','d'

UNION SELECT'E20','','','a','b','c','d'

UNION SELECT'E20','E2010','','a ','b','c','d'

UNION SELECT'E20','E2010','E201010','a','b','c','d'

UNION SELECT'E20','E2020','','a','b','c','d'

UNION SELECT'E20', 'E2020','E202010','a','b','c','d'

UNION SELECT'E20','E2020','E202020','a',' b','c','d'

GO



我试图使用FOR XML PATH为样本数据生成xml。当记录增加到几百个时,这不是一个好主意。



有没有人有更好的解决方案呢?谢谢。



Tao



I create a sample Src table:
CREATE TABLE Src
(
EL1 VARCHAR(10),
EL2 VARCHAR(10),
EL3 VARCHAR(10),
Attr01 VARCHAR(10),
Attr02 VARCHAR(10),
Attr03 VARCHAR(10),
Attr04 VARCHAR(10)
)
GO
INSERT INTO Src
(EL1,EL2,EL3,Attr01,Attr02,Attr03,Attr04
)
SELECT 'E10','','','a','b','c','d'
UNION SELECT 'E10','E1010','','a','b','c','d'
UNION SELECT 'E10','E1010','E101010','a','b','c','d'
UNION SELECT 'E10','E1010','E101020','a','b','c','d'
UNION SELECT 'E10','E1010','E101030','a','b','c','d'
UNION SELECT 'E10','E1020','','a','b','c','d'
UNION SELECT 'E10','E1020','E102010','a','b','c','d'
UNION SELECT 'E20','','','a','b','c','d'
UNION SELECT 'E20','E2010','','a','b','c','d'
UNION SELECT 'E20','E2010','E201010','a','b','c','d'
UNION SELECT 'E20','E2020','','a','b','c','d'
UNION SELECT 'E20','E2020','E202010','a','b','c','d'
UNION SELECT 'E20','E2020','E202020','a','b','c','d'
GO

I tried to use FOR XML PATH to generate xml for the sample data. When the records increase to a few hundreds, it's not a good idea.

Does anyone have better solution for this? Thanks.

Tao

推荐答案

如果你的SQL Server中有太多的记录来生成XML,我想你应该在代码中做到这一点。我不清楚,这不是一个好主意是什么意思?它不起作用吗?它超级慢吗?
If you have too many records to generate XML in your SQL Server, you should do it in code, I guess. I am unclear, what does 'it's not a good idea' mean ? Does it not work ? Is it super slow ?


这篇关于使用带有层次结构的表的FOR XML PATH生成XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆