1

how would you write a t-sql query if the namespace is prefixed in all of the elements? I've tried many variations, and this is what I've got so far, but it just doesn't work...

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)

Assistance will be greatly appreciated !

Here is the XML input file:

<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>

Expected output would be

orderReference,     orderNumber,    orderUserEmail
10027,      425816,              user@domain.com
brynn
  • 107
  • 1
  • 2
  • 11

2 Answers2

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)
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    Wow! thanks! that worked! I just had to add the prefix to the elements too to get it to work. 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 May 24 '12 at 13:14
  • @brynn Right, I forgot to do that. Answer modified. – Tomalak May 24 '12 at 13:17
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
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281