Sql server 用Soap信封查询sqlserver中的xml

Sql server 用Soap信封查询sqlserver中的xml,sql-server,xml,soap,Sql Server,Xml,Soap,我发现了许多在xml字段中处理soap名称空间的示例,但没有一个对我的案例有帮助: 我试图在下面的xml示例中查询单个元素。(我截断了示例,因此您不会看到结束元素,但它的格式正确。) 以下是xml: <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:tns="http://www.ex

我发现了许多在xml字段中处理soap名称空间的示例,但没有一个对我的案例有帮助:

我试图在下面的xml示例中查询单个元素。(我截断了示例,因此您不会看到结束元素,但它的格式正确。)

以下是xml:

<soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"     xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:tns="http://www.example.org/TransactionDataSchema" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PRPA_IN201305UV02 xmlns="urn:hl7-org:v3" ITSVersion="XML_1.0">
<id extension="123abc-12a3-123a-a1bc-0a0a0a0a0a0a" root="1.23.456.7.890123.4.5678.901.2.3.4" />
<creationTime value="20150602175238" />
<interactionId extension="PRPA_IN201305UV02" root="1.23.456.7.890123.4.5" />
<processingCode code="P" />
<processingModeCode code="T" />
<acceptAckCode code="AL" />
<receiver typeCode="RCV">
  <device classCode="DEV" determinerCode="INSTANCE">
    <id root="1.23.456.7.890123.4.5678.001.1" />
    <telecom value="https://stage.mysite.org:1111/myevent" />
etc …
以下是我在上面的原始查询中遇到的错误:

Msg 2229, Level 16, State 1, Line 19
XQuery [SW_Reporting.dbo.Client_Request.Request_Body.value()]: The name "soapenv" does not denote a namespace.

我尝试将
与xmlnamespaces一起使用
,我得到了一个选择,只使用“/”返回整个xml。在第二个元素(
之后,我得到了所有内容,但您遗漏了一个事实,即子元素隐式继承了祖先的默认名称空间,除非另有指定。这意味着,在这个特定的XML中,所有没有前缀的元素都位于默认名称空间中,uri是
“urn:hl7 org:v3”
,您需要使用相应的前缀来引用XPath/XQuery中的这些元素:

WITH xmlnamespaces (
'http://www.w3.org/2003/05/soap-envelope' as soapenv   
,'urn:hl7-org:v3' as ns1)
SELECT Data.query
('/soapenv:Body/ns1:PRPA_IN201305UV02/ns1:creationTime') as test
FROM @Table ;                       --^^^
                                    --Notice `ns1` prefix added here

这很有效。非常感谢!我需要一路指定前缀。虽然我得到了解决方案,但我仍然对您如何编写答案感到有点困惑。似乎您说过,没有指定前缀的任何元素都会获得默认前缀,在本例中为
urn:hl7 org:v3
。但是,这也是ns1的值,看起来我用相同的字符串替换默认值。我缺少什么?
WITH xmlnamespaces (
'http://www.w3.org/2003/05/soap-envelope' as soapenv
,'urn:hl7-org:v3' as ns1)
declare @table table (data xml);
insert into @table values (@xml);

WITH xmlnamespaces (
'http://www.w3.org/2003/05/soap-envelope' as soapenv   
,'urn:hl7-org:v3' as ns1)
SELECT Data.query
('/soapenv:Body/ns1:PRPA_IN201305UV02/creationTime') as test
FROM @Table ;
WITH xmlnamespaces (
'http://www.w3.org/2003/05/soap-envelope' as soapenv   
,'urn:hl7-org:v3' as ns1)
SELECT Data.query
('/soapenv:Body/ns1:PRPA_IN201305UV02/ns1:creationTime') as test
FROM @Table ;                       --^^^
                                    --Notice `ns1` prefix added here