1

如果命名空间在所有元素中都有前缀,您将如何编写 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>

预期输出为

orderReference,     orderNumber,    orderUserEmail
10027,      425816,              user@domain.com
4

2 回答 2

2
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)
于 2012-05-24T12:45:05.537 回答
1
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
于 2012-05-24T13:47:08.547 回答