我有这样的事情。
DECLARE @XML XML
SET @XML = CONVERT (XML, '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://domain1.net/">
<Result>Success</Result>
<info>
<task>
<Dt>01/7/2011</Dt>
<ref>
<RefNo>123456789</RefNo>
</ref>
</task>
</info>
</Response>
</soap:Body>
</soap:Envelope>
')
如何获得RefNo的值?
我尝试了这些,但它没有用。
SELECT DISTINCT
c.value('.', 'VARCHAR(MAX)') AS O_CaseID
, GETDATE() Create_Dt
FROM @XML.nodes('//RefNo') t(c)
SELECT DISTINCT
c.value('(//RefNo.)[1]', 'VARCHAR(MAX)') AS O_CaseID
, GETDATE() Create_Dt
FROM @XML.nodes('//.') t(c)
有什么想法吗?我需要使用local-name()吗?但它对我不起作用。
答案 0 :(得分:4)
您的XML文档中有XML命名空间 - 您需要使用 - 而不是忽略它们!
尝试此查询:
-- define the two relevant XML namespaces for your query
WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS soap,
'http://domain1.net/' AS ns)
SELECT
c.value('(ns:RefNo)[1]', 'VARCHAR(50)') AS O_CaseID
FROM
-- USE those namespaces as needed!
@XML.nodes('/soap:Envelope/soap:Body/ns:Response/ns:info/ns:task/ns:ref') t(c)