Tsql T-SQL中的XML名称空间

Tsql T-SQL中的XML名称空间,tsql,sql-server-2005,xml-namespaces,Tsql,Sql Server 2005,Xml Namespaces,如果名称空间在所有元素中都有前缀,您将如何编写t-sql查询?我已经尝试了很多变化,这是我到目前为止得到的,但它就是不起作用 DECLARE @x xml SET @x = (SELECT xml_data_column FROM dbo.Table WHERE xmlFileName = 'E:\trnFile.xml' ) ;WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS [soap] , 'tns:Re

如果名称空间在所有元素中都有前缀,您将如何编写t-sql查询?我已经尝试了很多变化,这是我到目前为止得到的,但它就是不起作用

DECLARE @x xml 
SET @x = (SELECT xml_data_column FROM dbo.Table
WHERE xmlFileName = 'E:\trnFile.xml'  )
;WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS [soap]
, 'tns:RetrievePurchaseResponse  xmlns:tns="urn:Transaction"' AS tns) 
SELECT t.c.value('orderRef[1]', 'int') orderReference
, t.c.value('orderNumber[1]', 'varchar(100)') orderNumber
, t.c.value('subtotal[1]', 'varchar(100)') subtotal
FROM  @x.nodes('/soap:Envelope/soap:Body/tns:RetrievePurchaseResponse/tns:purchase') AS t(c)
非常感谢您的帮助

以下是XML输入文件:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<tns:RetrievePurchaseResponse xmlns:tns="urn:Transaction">
  <tns:purchase>
    <tns:orderRef>10027</tns:orderRef>
        <tns:orderNumber>425816</tns:orderNumber>
        <tns:subtotal>95.00</tns:subtotal>
</tns:purchase>
    </tns:RetrievePurchaseResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
declare@xxml
设置@x='1
10027
425816
95
';
使用xmlnamespaces('urn:Transaction'作为tns)
选择
@X.value(“(//tns:RetrievePurchaseResponse/tns:purchase/tns:orderRef)[1],“int”)orderReference,
@x、 值('(//tns:RetrievePurchaseResponse/tns:purchase/tns:orderNumber)[1],'int')orderNumber,
@x、 值('(//tns:RetrievePurchaseResponse/tns:purchase/tns:orderUserEmail)[1],'varchar(100)'orderUserEmail

请发布您正在处理的XML示例以及使用该XML的查询的预期输出。哇!谢谢成功了!我只需要在元素中添加前缀就可以了。ie tns:t.c.value('tns:orderRef[1],'int')orderReference,t.c.value('tns:orderNumber[1],'varchar(100'))orderNumber,t.c.value('tns:subtotal[1],'varchar(100'))subtotal@brynn对,我忘了做那件事。修改答案。
orderReference,     orderNumber,    orderUserEmail
10027,      425816,              user@domain.com
DECLARE @x xml

SET @x = (
  SELECT xml_data_column 
    FROM dbo.Table 
   WHERE xmlFileName = 'E:\trnFile.xml'
);

WITH XMLNAMESPACES(
  'urn:Transaction' AS tns
) 
SELECT 
  t.c.value('tns:orderRef[1]', 'int') orderReference
, t.c.value('tns:orderNumber[1]', 'varchar(100)') orderNumber
, t.c.value('tns:subtotal[1]', 'varchar(100)') subtotal
FROM  
  @x.nodes('//tns:RetrievePurchaseResponse/tns:purchase') AS t(c)
declare @x xml 
set @x = '
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <tns:RetrievePurchaseResponse xmlns:tns="urn:Transaction">
      <tns:purchase>
        <tns:orderRef>10027</tns:orderRef>
        <tns:orderNumber>425816</tns:orderNumber>
        <tns:subtotal>95.00</tns:subtotal>
      </tns:purchase>
    </tns:RetrievePurchaseResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>';

with xmlnamespaces('urn:Transaction' as tns) 
select
  @X.value('(//tns:RetrievePurchaseResponse/tns:purchase/tns:orderRef)[1]', 'int') orderReference,
  @x.value('(//tns:RetrievePurchaseResponse/tns:purchase/tns:orderNumber)[1]', 'int') orderNumber,
  @x.value('(//tns:RetrievePurchaseResponse/tns:purchase/tns:orderUserEmail)[1]', 'varchar(100)') orderUserEmail