我想从以下XML中读取ContactId和Notes。我尝试使用Openxml并声明命名空间,但我无法获得预期的结果。请求您提供解决方案。
<ArrayOfContact xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">100</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test1</Notes>
</Contact>
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">101</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test2</Notes>
</Contact>
</ArrayOfContact>
答案 0 :(得分:2)
OPENXML
函数是SQL Server 2000的遗留XML支持,我使用xml
数据类型中引入的更新的XML方法。
DECLARE @x xml;
SET @x = '<ArrayOfContact xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">100</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test1</Notes>
</Contact>
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">101</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test2</Notes>
</Contact>
</ArrayOfContact>';
WITH XMLNAMESPACES ('http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData' AS cms)
SELECT
contact.value('cms:ContactId[1]', 'int') ContactId,
contact.value('cms:Notes[1]', 'nvarchar(MAX)') Notes
FROM @x.nodes('/ArrayOfContact/Contact') AS contacts(contact);