繁体   English   中英

从 SQL 中的 XML object 获取命名空间

[英]Fetch namespace from XML object in SQL

考虑以下 XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" s:mustUnderstand="1">http://xmlns.scania.com/management/messages/v3</Action>
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Data xmlns="http://xmlns.scania.com/management/schema/messages/v3">
      <Details />
    </Data>
  </s:Body>
</s:Envelope>

我正在尝试在信封下获取标记的 xmlns 值中的命名空间值。 我尝试使用

XMLObject.value('namespace-uri((/*:Envelope)[1])', 'varchar(100)')这将返回第一个元素的 xmlns。 "http://schemas.xmlsoap.org/soap/envelope/"

我需要深入到标签的 xmlns。 "http://xmlns.scania.com/management/schema/messages/v3"

有人可以帮我解决这个问题吗?

以下是 select 您的命名空间 URI 的三种方法...

declare @xml xml = N'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" s:mustUnderstand="1">http://xmlns.scania.com/management/messages/v3</Action>
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Data xmlns="http://xmlns.scania.com/management/schema/messages/v3">
      <Details />
    </Data>
  </s:Body>
</s:Envelope>';

-- Avoid this: wildcard namespaces are slow...
select Envelope.value('namespace-uri(.)', 'nvarchar(max)') as NamespaceUri
from @xml.nodes('/*:Envelope/*:Body/*:Data') soap(Envelope);

-- Declare namespace(s) inside the XPath expression...
select Envelope.value('namespace-uri(.)', 'nvarchar(max)') as NamespaceUri
from @xml.nodes('declare namespace soap="http://schemas.xmlsoap.org/soap/envelope/";
/soap:Envelope/soap:Body/*:Data') soap(Envelope);

-- Declare namespace(s) before the query (these would be usable in FOR XML as well)...
with xmlnamespaces(
  'http://schemas.xmlsoap.org/soap/envelope/' as soap
)
select Envelope.value('namespace-uri(.)', 'nvarchar(max)') as NamespaceUri
from @xml.nodes('/soap:Envelope/soap:Body/*:Data') soap(Envelope);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM