[英]SQL 2008 SP1 XML Value from ntext Column
I have a column that contains the following xml: 我有一列包含以下xml:
<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="5.1" xsi:type="ArrayOfKeyAnyValue">
<KeyAnyValue xsi:type="KeyAnyValue">
<key>1</key>
<value xsi:type="xsd:string">naa.6019cbc1a09e175d370df5320b00803a</value>
</KeyAnyValue>
<KeyAnyValue xsi:type="KeyAnyValue">
<key>2</key>
<value xsi:type="xsd:string">6215</value>
</KeyAnyValue>
<KeyAnyValue xsi:type="KeyAnyValue">
<key>3</key>
<value xsi:type="xsd:string">1291898</value>
</KeyAnyValue>
</obj>
I wish to return each of the key values as a separate column. 我希望将每个键值作为单独的列返回。 I have tried:
我努力了:
SELECT [table].[column1]
,CONVERT(xml, [table].[column1]).value('(/obj/KeyAnyValue[1]/value)[1]', 'nvarchar(max)') As KeyValue
from [table]
This just returns NULL. 这只是返回NULL。 Have I got the XML path right?
我有正确的XML路径吗?
Sure something simple I am doing wrong and much appreciate any help. 确保简单的事情我做错了,非常感谢您的帮助。
Thanks 谢谢
Your XPath expression has to say what namespace it's working with: 您的XPath表达式必须说出它正在使用什么名称空间:
declare @data xml = '<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:vim25" versionId="5.1"
xsi:type="ArrayOfKeyAnyValue">
<KeyAnyValue xsi:type="KeyAnyValue">
<key>1</key>
<value xsi:type="xsd:string">naa.6019cbc1a09e175d370df5320b00803a</value>
</KeyAnyValue>
<KeyAnyValue xsi:type="KeyAnyValue">
<key>2</key>
<value xsi:type="xsd:string">6215</value>
</KeyAnyValue>
<KeyAnyValue xsi:type="KeyAnyValue">
<key>3</key>
<value xsi:type="xsd:string">1291898</value>
</KeyAnyValue>
</obj>'
select @data.value('declare namespace a="urn:vim25";
(/a:obj/a:KeyAnyValue[1]/a:value)[1]', 'nvarchar(max)')
Produces: 生产:
naa.6019cbc1a09e175d370df5320b00803a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.