MS SQL 2005 表到 XML
我在 SQL Server 2005 中有一个简单的表,我希望将其转换为 XML(使用“FOR XML”子句)。 我无法让 XML 看起来像所需的输出。
我尝试在网上浏览各种教程,但我很挣扎。 有人可以帮忙吗?
我的表格看起来像这样
TYPE,GROUP,VALUE
Books,Hardback,56
Books,Softcover,34
CDs,Singles,45
CDS,Multis,78
我需要的输出样式是:
<data>
<variable name="TYPE">
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
</variable>
<variable name="TYPE">
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
</variable>
</data>
编辑: 据我所知,我需要多个值。 我正在生成用于 Xcelsius 的 XML(链接 XML 和 Xcelsius )因此无法控制 XML 的格式。 我可以按照链接教程使用 ASP 生成 XML,但我希望直接从 SQL Server 获取它。
编辑2: 我希望有一些优雅和整洁的东西......但戈德克的例子最接近。 一些摆弄 SQL,我想出了:
select
"type" as '@name',
"group" as 'row/column',
null as 'row/tmp',
"value" as 'row/column'
from tableName
for xml path('variable'), root('data')
几乎以我想要的方式输出。 null/tmp 行甚至不输出; 它只是阻止串联。 标签
仍然对每一行重复,这是我无法拥有的。
I have a simple table in SQL Server 2005, I wish to convert this to XML (using the "FOR XML" clause). I'm having trouble getting my XML to look like the required output.
I've tried looking through various tutorials on the web, but I am struggling. Can someone help?
The table I have looks like this
TYPE,GROUP,VALUE
Books,Hardback,56
Books,Softcover,34
CDs,Singles,45
CDS,Multis,78
The output style I need is:
<data>
<variable name="TYPE">
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
</variable>
<variable name="TYPE">
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
<row>
<column>GROUP</column>
<column>VALUE</column>
</row>
</variable>
</data>
Edit:
As far as I can tell I require the multiple values. I'm generating XML for use with Xcelsius (Linking XML and Xcelsius) so have no control over in the formatting of the XML. I can generate the XML using ASP as per the linked tutorial, but I was hoping to get it straight from SQL Server.
Edit 2:
I was hoping for something elegant and tidy... but Godeke's example got the closest. Some fiddling with the SQL and I've come up with:
select
"type" as '@name',
"group" as 'row/column',
null as 'row/tmp',
"value" as 'row/column'
from tableName
for xml path('variable'), root('data')
Outputs almost in the exact way I wanted. The null/tmp line doesn't even output; it is just preventing the concatenation. Still the tag <variable name="TYPE">
repeats for each row, which I can't have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我所能得到的最接近的是:
将两个项目命名为相同的(“column”和“column”)并不是我知道如何一次完成的事情,但另一方面,这是一个奇怪的 XML 模式选择; 通常,如果元素包含不同的数据,则它们具有唯一的名称。 显而易见的选择(将它们都命名为“行/列”)只是将输出中的它们连接成一个值。
另请注意,每个返回的行都将是一个与其他行不同的“变量”元素。 要获得没有冗余记录的嵌套,将需要子查询:
这是我的第一个想法,但不同的会阻止嵌套。
所有这些让我认为,要获得您需要的准确输出,您可能必须使用 EXPLICIT 模式。 也许我的问题是这样的,我在代码中使用 DOMDocument :)。
As close as I can get is this:
Naming two items the same ("column" and "column") isn't something I know how to do in one pass, but on the other hand it is an odd XML schema choice; normally elements have unique names if they contain distinct data. The obvious choice (name them both 'row/column') simply concatenates them in the output into one value.
Also note that each returned row will be a "variable" element distinct from the others. To get the nesting without redundant records will require a subquery:
was my first thought, but the distinct prevents nesting.
All this makes me think that to get the exact output you need you might have to use EXPLICIT mode. Perhaps my problem is for something like this I punt and use a DOMDocument in code :).
我更喜欢使用 XML PATH,它提供了一种更好的方法来控制元素等。
参见
但这很棘手,
无法满足您的要求,但它并不优雅和整洁。
I prefer using for XML PATH, it provides a nicer way to control your elements etc.
See
But this is quite tricky
gives you what you are asking for I, but elegant and tidy it is not.
下面的脚本生成所需的格式
调用
哪里
The script below produces the desired format
Invoke
where