4

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/">
      <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 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.