Get elements of same name from XML using XQuery in SQL server

I am having following XML

DECLARE @ruleXML XML
SET @RuleXML = '<questionnaire xmlns:xsi="http://schema1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schem2" title="Sample">
  <sections>
    <section id="24" title="Section Title" help="" url="">
      <questions />
    </section>
    <section id="23" title="Information" help="" url="">
      <questions />
    </section>
    <section id="25" title="Section Title1" help="" url="">
      <questions>
        <question id="3" title="Question Text">
          <display-rules />
          <questions />
        </question>
        <question id="4" title="Question Text" >
          <response-set type="inline" />
          <display-rules />
          <questions />
        </question>
      </questions>
    </section>
  </sections>
</questionnaire>'

How to get a table with question id and title from all the question nodes regardless of their level using XQUERY in SQL server?

Answers


; with  xmlnamespaces (default 'http://schem2')
select  tbl.col1.value('@id', 'int')
,       tbl.col1.value('@title', 'varchar(100)')
from    @RuleXML.nodes('//question') tbl(col1)

Working example at SQL FIddle.


Need Your Help

Change URL Routing in Angularjs

angularjs

I am Working On AngularJs, If I Type On Browser like

Ctype casting to Decimal returning negative value

vb.net casting decimal type-conversion ctype

I am wondering if this is a bug. Can't understand why would CType work this way...!