[英]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.