我有一个相当大的查询(十几个表中的25个标签; ~1500行)需要使用FOR XML EXPLICIT格式化为XML。不幸的是,这个文件的使用者要求使用CDATA标签,否则我会使用FOR XML PATH。
这就是我正在寻找的:
<elem elem_att1="..." elem_att2="1" elem_att3="...">
<a>...</a>
<b>...</b>
<c>...</c>
<d>...</d>
<e>
<e1 e1_att1="..." e1_att2="1">
<e11>...</e11>
<e12><![CDATA[...]]></e12>
<e13><![CDATA[...]]></e13>
</e1>
<e1 e1_att1="..." e1_att2="2">
<e11>...</e11>
<e12><![CDATA[...]]></e12>
<e13><![CDATA[...]]></e13>
</e1>
<e1 e1_att1="..." e1_att2="3">
<e11>...</e11>
<e12><![CDATA[...]]></e12>
<e13><![CDATA[...]]></e13>
</e1>
</e>
<f>
<f1 f1_att1="..." f1_att2="..." />
<f2 f2_att1="..." f2_att2="..." />
<f3 f3_att1="..." f3_att2="..." />
</f>
</elem>
假设在查询中正确定义了以下标记和父映射:
Tag | Parent
elem 2 | 1
a 3 | 2
b 4 | 2
c 5 | 2
d 6 | 2
e 7 | 2
e1 8 | 7
e11 9 | 8
e12 10 | 8
e13 11 | 8
f 12 | 2
f1 13 | 12
f2 14 | 12
f3 15 | 12
我已经获得了大约90%的查询方式,但遇到了与通用表中的标签顺序相关的问题。我希望看到从查询返回的表的前两列的以下输出:
Tag | Parent
2 | 1
3 | 2
4 | 2
5 | 2
6 | 2
7 | 2 <-- beginning of the "e" element
8 | 7 <-- first instance of the "e1" element
9 | 8
10 | 8
11 | 8
8 | 7 <-- second instance of the "e1" element
9 | 8
10 | 8
11 | 8
8 | 7 <-- third and final instance of the "e1" element
9 | 8
10 | 8
11 | 8
12 | 2 <-- beginning of the "f" element
13 | 12
14 | 12
15 | 12
相反,我将此作为查询输出的前两列:
Tag | Parent
2 | 1
3 | 2
4 | 2
5 | 2
6 | 2
7 | 2 <-- beginning of the "e" element
12 | 2 <-- beginning of the "f" element
13 | 12
14 | 12
15 | 12
8 | 7 <--first instance of the "e1" element
9 | 8
10 | 8
11 | 8
8 | 7 <-- second instance of the "e1" element
9 | 8
10 | 8
11 | 8
8 | 7 <-- third and final instance of the "e1" element
9 | 8
10 | 8
11 | 8
这显然会产生格式错误的XML:
<elem elem_att1="..." elem_att2="1" elem_att3="...">
<a>...</a>
<b>...</b>
<c>...</c>
<d>...</d>
<e />
<f>
<f1 f1_att1="..." f1_att2="..." />
<f2 f2_att1="..." f2_att2="..." />
<f3 f3_att1="..." f3_att2="..." />
</f>
<e1 e1_att1="..." e1_att2="1">
<e11>...</e11>
<e12><![CDATA[...]]></e12>
<e13><![CDATA[...]]></e13>
</e1>
<e1 e1_att1="..." e1_att2="2">
<e11>...</e11>
<e12><![CDATA[...]]></e12>
<e13><![CDATA[...]]></e13>
</e1>
<e1 e1_att1="..." e1_att2="3">
<e11>...</e11>
<e12><![CDATA[...]]></e12>
<e13><![CDATA[...]]></e13>
</e1>
</e>
</elem>
查询会阻塞并抛出以下错误:
Msg 6833, Level 16, State 1, Line 2
Parent tag ID 7 is not among the open tags. FOR XML EXPLICIT requires parent tags to be opened first. Check the ordering of the result set.
这是我的order by
条款:
order by [elem!2!att2], [e1!8!att2]
如果我将order by
子句更改为
order by [elem!2!att2], tag, [e1!8!att2]
查询成功执行,但e1
元素的所有子记录都嵌套在最后e1
元素下:
<elem elem_att1="..." elem_att2="1" elem_att3="...">
<a>...</a>
<b>...</b>
<c>...</c>
<d>...</d>
<e>
<e1 e1_att1="..." e1_att2="1" />
<e1 e1_att1="..." e1_att2="2" />
<e1 e1_att1="..." e1_att2="3">
<e11>...</e11>
<e11>...</e11>
<e11>...</e11>
<e12><![CDATA[...]]></e12>
<e12><![CDATA[...]]></e12>
<e12><![CDATA[...]]></e12>
<e13><![CDATA[...]]></e13>
<e13><![CDATA[...]]></e13>
<e13><![CDATA[...]]></e13>
</e1>
</e>
<f>
<f1 f1_att1="..." f1_att2="..." />
<f2 f2_att1="..." f2_att2="..." />
<f3 f3_att1="..." f3_att2="..." />
</f>
</elem>
实际问题:
导致f
及其子女在e
个孩子的人口之前填入结果集的原因是什么?
我希望这是一个相对常见的错误,其解决方案足够抽象,无需复制超过1500行代码即可进行中继。
答案 0 :(得分:0)
很长一段时间以来,我已经使用了这个,但你试过吗
order by [elem!2!att2], [e], [e1!8!att2]
或
order by [elem!2!att2], 7, [e1!8!att2]