【问题标题】:How can I select tags from an SQL XML Query?如何从 SQL XML 查询中选择标签?
【发布时间】:2011-03-22 22:33:21
【问题描述】:

如何在 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]

【问题讨论】:

  • 你试过在信封和身体前加肥皂吗:?

标签: sql xml sql-server-2005 xquery


【解决方案1】:

您忽略了正在运行的 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]

希望能奏效!

【讨论】:

  • 谢谢 Marc_S!这是完美的!无需修改。
猜你喜欢
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多