views:

917

answers:

1

Hello,

I'm trying to query fields from the following XML query, (which is really a web service call):

<soap:Envelope xmlns:xsi="[schema]" xmlns:xsd="[shema]" xmlns:soap="[schema]">
  <soap:Body>
    <RunPackage xmlns="http://tempuri.org/"&gt;
      <xmlDoc>
        <Request>
          <SubscriberCode>543253</SubscriberCode>
          <CompanyCode>54325</CompanyCode>
          <BranchName>TestBranchName</BranchName>
          <TempWorksUserName>TempWorksUserName</TempWorksUserName>
[...]

With the following XML Query:

WITH XMLNAMESPACES('[schema]' AS soap2, DEFAULT '[schema]')

SELECT TransactionID, T2.Loc.query('data(Request/SubscriberCode)') as 'SubscriberCode'
FROM TempWorksRequest
CROSS APPLY RequestXML.nodes('soap2:Envelope/soap2:Body/RunPackage/xmlDoc') as T2(Loc)

It runs but does not return any retults!

If I build the same query but remove the namespace stuff THEN it works. For example, the following works fine:

<xmlDoc> <Request> <SubscriberCode>543253</SubscriberCode> <CompanyCode>54325</CompanyCode> <BranchName>TestBranchName</BranchName> [...]

SQL Query:

-- Define a namespace for MITS so we can use the MITS namespace. WITH XMLNAMESPACES('[schema]' AS soap2, DEFAULT '[schema]')

SELECT TransactionID, T2.Loc.query('data(Request/SubscriberCode)') as 'SubscriberCode' FROM TempWorksRequest CROSS APPLY RequestXML.nodes('xmlDoc') as T2(Loc)

Any ideas?

+1  A: 

Found the issue thanks for Mark! Namespaces must be explicitly declared.

New WORKING query:

WITH XMLNAMESPACES('[URI1]' AS ns, '[URI2]' AS soap) 

SELECT TransactionID, 
    T2.Loc.query('data(ns:SubscriberCode)') as 'SubscriberCode',
FROM TempWorksRequest
CROSS APPLY RequestXML.nodes('soap:Envelope/soap:Body/ns:RunPackage/ns:xmlDoc/ns:Request') as T2(Loc)
KTF