OPENXML、Xsi:nil 和小数
我有一些 XML,其中包含某些字符串和数字元素的 xsi:nil="true" 。下面是一个示例:
declare @data xml
set @data = '<?xml version="1.0" encoding="utf-8"?>
<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<item>
<stringprop1>foo</stringprop1>
<stringprop2 xsi:nil="true" />
<decimalprop3 xsi:nil="true" />
</item>
</collection>'
我想在 SQL Server 2008 R2 中查询该 XML。我正在使用 OPENXML,但它不能很好地处理十进制类型。这是我想编写的代码:
declare @doc int
exec sp_xml_preparedocument @doc OUTPUT, @data;
select
stringprop1,
stringprop2,
decimalprop3
from openxml(@doc, '/collection/item', 2)
with
(
stringprop1 nvarchar(50)
,stringprop2 nvarchar(50)
,decimalprop3 decimal(18, 5)
)
exec sp_xml_removedocument @doc;
这抱怨将 nvarchar 转换为十进制。经过一番摸索后,我得出了这样的结论:
exec sp_xml_preparedocument @doc OUTPUT, @data;
select
nullif(stringprop1, '') as stringprop1,
nullif(stringprop2, '') as stringprop2,
convert(decimal(18, 5), nullif(decimalprop3, '')) as decimalprop3
from openxml(@doc, '/collection/item', 2)
with
(
stringprop1 nvarchar(50)
,stringprop2 nvarchar(50)
,decimalprop3 nvarchar(50)
)
exec sp_xml_removedocument @doc;
我想这很好。但是有什么方法可以告诉 OPENXML xsi:nil 表示 NULL 并且这对于小数和字符串都可以吗?
I have some XML containing xsi:nil="true" for certain string and numeric elements. Here's an example:
declare @data xml
set @data = '<?xml version="1.0" encoding="utf-8"?>
<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<item>
<stringprop1>foo</stringprop1>
<stringprop2 xsi:nil="true" />
<decimalprop3 xsi:nil="true" />
</item>
</collection>'
I want to query that XML in SQL Server 2008 R2. I'm using OPENXML but it's not playing nicely with decimal types. Here's the code I'd like to write:
declare @doc int
exec sp_xml_preparedocument @doc OUTPUT, @data;
select
stringprop1,
stringprop2,
decimalprop3
from openxml(@doc, '/collection/item', 2)
with
(
stringprop1 nvarchar(50)
,stringprop2 nvarchar(50)
,decimalprop3 decimal(18, 5)
)
exec sp_xml_removedocument @doc;
This complains about converting nvarchar to decimal. After some hacking around, I arrived at this:
exec sp_xml_preparedocument @doc OUTPUT, @data;
select
nullif(stringprop1, '') as stringprop1,
nullif(stringprop2, '') as stringprop2,
convert(decimal(18, 5), nullif(decimalprop3, '')) as decimalprop3
from openxml(@doc, '/collection/item', 2)
with
(
stringprop1 nvarchar(50)
,stringprop2 nvarchar(50)
,decimalprop3 nvarchar(50)
)
exec sp_xml_removedocument @doc;
Which is fine, I guess. But is there any way to tell OPENXML that xsi:nil means NULL and that that's ok for decimals as well as strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
xsi:nil 是一种 XML 模式功能,OpenXML 是在它出现之前就设计的,并且不支持 xsi:nil。由于您使用 SQL Server 2008,使其发挥作用的一种方法是:
使用适当的 XML 架构来约束 XML,该架构验证数据并识别 xsi:nil 并将其映射到空值。
使用nodes()和value()方法提取数据。
此致
迈克尔
xsi:nil is an XML Schema feature and OpenXML was designed before it's existance and does not support xsi:nil. Since you use SQL Server 2008, one way to make this to work is to:
constrain the XML with the appropriate XML Schema that validates the data and will recognize the xsi:nil and map it to the empty value.
use the nodes() and value() methods to extract the data.
Best regards
Michael
这应该做:
声明@doc int
exec sp_xml_preparedocument @doc 输出,@data,'<行 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
This should do:
Declare @doc int
exec sp_xml_preparedocument @doc OUTPUT, @data, '< row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />'
死灵术。
这对我有用:
如何创建字段列表:
Necromancing.
This is what works for me:
How to create the field-list: