SQL Server 2008 - 将 XML 声明添加到 XML 输出
我已经与这个问题斗争了几天了,我正在寻找使用以下语法自动化 XML 输出,
SELECT (
SELECT CONVERT(VARCHAR(10),GETDATE(),103)
FOR XML PATH('DataVersion'),
TYPE
),
(
SELECT CoNum,
CoName,
CONVERT(VARCHAR(10),AccountToDate,103) 'DLA',
LAFileNet
FROM @XMLOutput
FOR XML PATH('Company'),
TYPE
)
FOR XML PATH(''),
ROOT('Companies')
这会创建以下输出
<Companies>
<DataVersion>15/11/2010</DataVersion>
<Company>
<CoNum>111</CoNum>
<CoName>ABCLmt</CoName>
<DLA>12/12/2010</DLA>
<LAFileNet>1234</LAFileNet>
</Company>
<Company>
<CoNum>222</CoNum>
<CoName>DEFLmt</CoName>
<DLA>12/12/2007</DLA>
<LAFileNet>5678</LAFileNet>
</Company>
</Companies>
我正在努力解决的是如何添加 XML 声明 < ?xml version="1.0"encoding="ISO-8859-1" ?>
到输出顶部??
更新 1:我是否正确地认为我需要在 SQL Server 中创建 XML 模式来定义 xsl:output。然后将输出分配给该模式?
更新 2:已经找到这些链接 http://forums.asp.net/t/1455808.aspx -- 查看康健评论。还 http://www.devnewsgroups.net/group/microsoft.public .sqlserver.xml/topic60022.aspx
I've been battling with this one for a few days now, I'm looking to automate an XML output with the below syntax
SELECT (
SELECT CONVERT(VARCHAR(10),GETDATE(),103)
FOR XML PATH('DataVersion'),
TYPE
),
(
SELECT CoNum,
CoName,
CONVERT(VARCHAR(10),AccountToDate,103) 'DLA',
LAFileNet
FROM @XMLOutput
FOR XML PATH('Company'),
TYPE
)
FOR XML PATH(''),
ROOT('Companies')
Which creates the below output
<Companies>
<DataVersion>15/11/2010</DataVersion>
<Company>
<CoNum>111</CoNum>
<CoName>ABCLmt</CoName>
<DLA>12/12/2010</DLA>
<LAFileNet>1234</LAFileNet>
</Company>
<Company>
<CoNum>222</CoNum>
<CoName>DEFLmt</CoName>
<DLA>12/12/2007</DLA>
<LAFileNet>5678</LAFileNet>
</Company>
</Companies>
What I'm struggling with is how to add the XML declaration <?xml version="1.0" encoding="ISO-8859-1" ?>
to the top of the output??
Update 1: Would I be correct in thinking I need to create a XML schema in SQL server to define the xsl:output. Then assign the output to that schema?
Update 2: Have since found these links
http://forums.asp.net/t/1455808.aspx -- Check out comment from Jian Kang. Also
http://www.devnewsgroups.net/group/microsoft.public.sqlserver.xml/topic60022.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TL;DR
将以下内容连接:
与您的 XML,转换为 varchar(max)。
详细信息
我同意 j0N45 架构不会改变任何内容。正如他引用的答案指出的:
我在另一个答案中提供了一些示例代码。基本上,您将 XML
CONVERT
为varchar
或nvarchar
,然后将其与 XML 声明连接起来,例如。
然而,选择正确的编码很重要。 SQL Server 根据其排序规则设置生成非 Unicode 字符串。默认情况下,这将由数据库排序规则设置控制,您可以使用以下 SQL 确定该设置:
常见的默认排序规则是“SQL_Latin1_General_CP1_CI_AS”,其代码页为 1252。您可以使用以下 SQL 检索代码页:
对于代码页 1252,您应该使用编码名称“windows-1252"。 “ISO-8859-1”的使用不准确。您可以使用“项目符号”字符进行测试: •。它的 Unicode 代码点值为 8226(十六进制 2022)。使用以下代码,无论排序规则如何,您都可以可靠地在 SQL 中生成字符:
它在 windows-1252 代码页中也有一个代码点 149,因此,如果您使用的是“SQL_Latin1_General_CP1_CI_AS”的常见默认排序规则,那么您也可以使用以下方法生成它:
但是,CHAR(149) 不会是所有排序规则中的项目符号。例如,如果您尝试这样做:
您根本不会得到子弹。
“ISO-8859-1”代码页是 Windows-28591。 SQL Server 排序规则(至少在 2005 年)都没有使用该代码页。您可以使用以下方法获取完整的代码页列表:
您可以通过尝试在 SQL 本身中使用“ISO-8859-1”来进一步验证它是否是错误的选择。以下 SQL:
将生成不包含项目符号的 XML。事实上,它不会产生任何字符,因为 ISO-8859-1 没有为代码点 149 定义字符。SQL
Server 以不同的方式处理 Unicode 字符串。对于 Unicode 字符串 (
nvarchar
)," 没有需要不同的代码页来处理不同的字符集”。但是,SQL Server 不使用“UTF-8”编码。如果您尝试在 SQL 本身中使用它:您将收到错误:
相反,SQL 使用“UCS-2”编码,因此这将起作用:
TL;DR
Concatenate this:
<?xml version="1.0" encoding="windows-1252" ?>
with your XML, converted to varchar(max).Details
I agree with j0N45 that the schema will not change anything. As the answer he references points out:
I provided some example code to do so in another answer. Basically, you
CONVERT
the XML intovarchar
ornvarchar
and then concatenate it with the XML declaration, such as<?xml version="1.0" encoding="windows-1252" ?>
.However, it's important to choose the right encoding. SQL Server produces non-Unicode strings according to its collation settings. By default, that will be governed by the database collation settings, which you can determine using this SQL:
A common default collation is "SQL_Latin1_General_CP1_CI_AS", which has a code page of 1252. You can retrieve the code page with this SQL:
For code page 1252, you should use an encoding name of "windows-1252". The use of "ISO-8859-1" is inaccurate. You can test that using the "bullet" character: •. It has a Unicode Code Point value of 8226 (Hex 2022). You can generate the character in SQL reliably, regardless of collation, using this code:
It has also has a code point of 149 in the windows-1252 code page, so you if you are using the common, default collation of "SQL_Latin1_General_CP1_CI_AS", then you can also produce it using:
However, CHAR(149) won't be a bullet in all collations. For example, if you try this:
You don't get a bullet at all.
The "ISO-8859-1" code page is Windows-28591. None of the SQL Server collations (in 2005 anyway) use that code page. You can get a full list of code pages using:
You can further verify that "ISO-8859-1" is the wrong choice by trying to use it in SQL itself. The following SQL:
Will produce XML which does not contain a bullet. Indeed, it won't produce any character, because ISO-8859-1 has no character defined for code point 149.
SQL Server handles Unicode strings differently. With Unicode strings (
nvarchar
), "there is no need for different code pages to handle different sets of characters". However, SQL Server does NOT use "UTF-8" encoding. If you try to use it within SQL itself:You will get an error:
Rather, SQL uses "UCS-2" encoding, so this will work:
我认为这回答了你的问题 如何添加xml编码到 SQL Server 中的 xml 输出。
我不认为创建模式会改变任何事情,因为它仅用于验证。
干杯
I think this answers to your question How to add xml encoding <?xml version="1.0" encoding="UTF-8"?> to xml Output in SQL Server.
I don't think creating a schema would change anything, because it is only used to validation.
Cheers