如何从SQL XML查询中选择标记?

时间:2010-07-29 19:55:37

标签: sql xml sql-server-2005 xquery

如何在MS SQL中的XML字段中检索字段?

每当我使用此XML代码时,我尝试的每个查询都无法正常工作: 我想选择AccNumber值。

<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:Header>
    <AuthSoapHd xmlns="https://temp.uri.com/Mngmt/">
      <System>System1</System>
    </AuthSoapHd>
  </soap:Header>
  <soap:Body>
    <PrintList xmlns="https://temp.uri.com/Mngmt/">
      <Account>
        <AccNumber xmlns="https://temp.uri.com/Project/Object/Data">990368644</AccNumber>
      </Account>
    </PrintList>
  </soap:Body>
</soap:Envelope>

我尝试了以下几个没有成功的变种

Select [RequestXML].query('/Envelope/Body/PrintList/Account/AccNumber')
  FROM [dbo].[Table1]

1 个答案:

答案 0 :(得分:4)

您忽略了正在使用的XML命名空间 - 您需要注意这一点!

WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS soap,
                   'https://temp.uri.com/Project/Object/Data' AS data,
                   'https://temp.uri.com/Mngmt/' AS mgmt)

SELECT 
   RequestXML.value('(/soap:Envelope/soap:Body/mgmt:PrintList/mgmt:Account/data:AccNumber)[1]',
                    'BIGINT') AS 'AccNumber'
FROM 
   [dbo].[Table1]

希望有效!