简体   繁体   中英

T-SQL XQuery nested namespace issue

I just want an actualEndTime - is that so much to ask?

declare @x xml = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <bogus>1934</bogus>
    <getJobActionsResponse xmlns="urn:JobService">
      <getJobActionsReturn>
        <job xmlns:ns1="urn:JobService" xsi:type="ns1:SvcJob">
          <actions />
          <actualEndTime>
            <dateString>2019-05-15 19:46:54.207</dateString>
          </actualEndTime>
        </job>
      </getJobActionsReturn>
    </getJobActionsResponse>
  </soapenv:Body>
</soapenv:Envelope>'

It seems from posts like this that I should be able to do a query like this:

;with xmlnamespaces (N'http://schemas.xmlsoap.org/soap/envelope/' as ns0, 
                     N'urn:JobService' as ns2)
select @x.value('(/ns0:envelope/ns0:body/ns2:getJobActions/ns2:getJobActionsReturn/ns2:job/ns2:actualEndTime)[1]', 'nvarchar(max)')

I've tried various permutations of namespace prefixing, but everything I try returns null. I can't even get an upstream bogus value to return non-null:

;with xmlnamespaces (N'http://schemas.xmlsoap.org/soap/envelope/' as ns0)
select @x.value('(/ns0:envelope/ns0:body/ns0:bogus)[1]', 'nvarchar(max)')

I don't get namespaces (every web page explaining them seems to be a mile long). Sorry, please and thanks.

You're close, but you had these mistakes:

  • Tag names are case-sensitive - you need to use <ns0:Envelope> (not <ns0:envelope> ) etc.

  • You misspelled the <ns2:getJobActionsResponse> as <ns2:getJobActions>

  • You didn't go "all the way down" to the <ns2:dateString> element

Try this :

declare @x xml = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <bogus>1934</bogus>
    <getJobActionsResponse xmlns="urn:JobService">
      <getJobActionsReturn>
        <job xmlns:ns1="urn:JobService" xsi:type="ns1:SvcJob">
          <actions />
          <actualEndTime>
            <dateString>2019-05-15 19:46:54.207</dateString>
          </actualEndTime>
        </job>
      </getJobActionsReturn>
    </getJobActionsResponse>
  </soapenv:Body>
</soapenv:Envelope>'

;with xmlnamespaces (N'http://schemas.xmlsoap.org/soap/envelope/' as ns0, 
                     N'urn:JobService' as ns2)
select @x.value('(/ns0:Envelope/ns0:Body/ns2:getJobActionsResponse/ns2:getJobActionsReturn/ns2:job/ns2:actualEndTime/ns2:dateString)[1]', 'nvarchar(max)')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM