Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:01:19+00:00 2026-05-25T16:01:19+00:00

I have an XML document snippet that matches this XSD: <xs:complexType name=QuestionType> <xs:sequence> <xs:element

  • 0

I have an XML document snippet that matches this XSD:

<xs:complexType name="QuestionType">
  <xs:sequence>
    <xs:element name="questionId" type="xs:string" minOccurs="1" />
    <xs:element name="questionDescription" type="xs:string" minOccurs="1" />
    <xs:element name="questionHeader" type="xs:string" minOccurs="0" />
    <xs:element name="questionLabel" type="xs:string" minOccurs="0" />
    <xs:element name="version" type="xs:string" minOccurs="1" maxOccurs="1" />
    <xs:element name="SubQuestion" type="QuestionType"
                minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>

This recursively defines a <Question> elements that can have an infite number of <SubQuestion> elements, both of the type QuestionType.

Using SQL, I’d like to query the document once to get a single result set with all of the questions and sub-questions. I have two independent queries at the moment to achieve this (please note that I’m using NVarChar(1000) for testing purposes only – they will be more appropriately sized in production, and that @X is an XML variable that matches the schema above):

SELECT -- Top-level questions...
  C.value('questionId[1]', 'NVarChar(1000)') Id,
  NULL ParentId,
  C.value('questionDescription[1]', 'NVarChar(1000)') Description,
  NULLIF(C.value('questionHeader[1]', 'NVarChar(1000)'), '') Header,
  NULLIF(C.value('questionLabel[1]', 'NVarChar(1000)'), '') Label,
  C.value('version[1]', 'NVarChar(1000)') Version
FROM @X.nodes('//Question') X(C);

SELECT -- Sub-questions...
  C.value('questionId[1]', 'NVarChar(1000)') Id,
  C.query('..').value('(Question/questionId)[1]', 'NVarChar(1000)') ParentId,
  C.value('questionDescription[1]', 'NVarChar(1000)') Description,
  NULLIF(C.value('questionHeader[1]', 'NVarChar(1000)'), '') Header,
  NULLIF(C.value('questionLabel[1]', 'NVarChar(1000)'), '') Label,
  C.value('version[1]', 'NVarChar(1000)') Version
FROM @X.nodes('//SubQuestion') X(C);

I’d expect this could be solved using a recursive CTE, but I’m having trouble putting one together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:01:20+00:00Added an answer on May 25, 2026 at 4:01 pm

    Given that you have tagged this question with sql-server-2008 and that IMHO SQL Server 2008 has support for XQuery I would like to suggest a different “angle”: use an XPath expression to select the nodes you are interested in.

    .//*[local-name(.) = 'Question' or local-name(.) = 'SubQuestion']
    

    Please note that I am using the XPath function local-name() in case your real XML data has namespace declarations.

    I have created a sample XML file to test the expression above:

    <?xml version="1.0" encoding="UTF-8"?>
    <Questions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns="http://www.acme.com" xsi:schemaLocation="sample.xsd">
        <Question>
            <questionId>1</questionId>
            <questionDescription>Question 1</questionDescription>
            <version>1</version>
            <SubQuestion>
                <questionId>1.1</questionId>
                <questionDescription>Question 1.1</questionDescription>
                <version>1</version>
                <SubQuestion>
                    <questionId>1.1.1</questionId>
                    <questionDescription>Question 1.1.1</questionDescription>
                    <version>1</version>
                    <SubQuestion>
                        <questionId>1.1.1.1</questionId>
                        <questionDescription>Question 1.1.1.1</questionDescription>
                        <version>1</version>
                    </SubQuestion>
                    <SubQuestion>
                        <questionId>1.1.1.2</questionId>
                        <questionDescription>Question 1.1.1.2</questionDescription>
                        <version>1</version>
                    </SubQuestion>
                </SubQuestion>
                <SubQuestion>
                    <questionId>1.2</questionId>
                    <questionDescription>Question 1.2</questionDescription>
                </SubQuestion>
            </SubQuestion>
        </Question>
        <Question>
            <questionId>2</questionId>
            <questionDescription>Question 2</questionDescription>
            <version>1</version>
        </Question>
        <Question>
            <questionId>3</questionId>
            <questionDescription>Question 3</questionDescription>
            <version>1</version>
            <SubQuestion>
                <questionId>3.1</questionId>
                <questionDescription>Question 3.1</questionDescription>
                <version>1</version>
            </SubQuestion>
        </Question>
    </Questions>
    

    Evaluating this XQuery query against that sample

    declare namespace acme = "http://www.acme.com";
    <AllQuestions>
     {
       for $question in .//*[local-name(.) = 'Question' or local-name(.) = 'SubQuestion']
       return
            <Question>
                <questionId>{ data($question/acme:questionId) }</questionId>
                <questionDescription>{ data($question/acme:questionDescription) }</questionDescription>
            </Question>
     }
    </AllQuestions>
    

    will result in

    <?xml version="1.0" encoding="UTF-8"?>
    <AllQuestions>
       <Question>
          <questionId>1</questionId>
          <questionDescription>Question 1</questionDescription>
       </Question>
       <Question>
          <questionId>1.1</questionId>
          <questionDescription>Question 1.1</questionDescription>
       </Question>
       <Question>
          <questionId>1.1.1</questionId>
          <questionDescription>Question 1.1.1</questionDescription>
       </Question>
       <Question>
          <questionId>1.1.1.1</questionId>
          <questionDescription>Question 1.1.1.1</questionDescription>
       </Question>
       <Question>
          <questionId>1.1.1.2</questionId>
          <questionDescription>Question 1.1.1.2</questionDescription>
       </Question>
       <Question>
          <questionId>1.2</questionId>
          <questionDescription>Question 1.2</questionDescription>
       </Question>
       <Question>
          <questionId>2</questionId>
          <questionDescription>Question 2</questionDescription>
       </Question>
       <Question>
          <questionId>3</questionId>
          <questionDescription>Question 3</questionDescription>
       </Question>
       <Question>
          <questionId>3.1</questionId>
          <questionDescription>Question 3.1</questionDescription>
       </Question>
    </AllQuestions>
    

    EDIT – Final Query

    SELECT
        C.value('questionId[1]', 'NVarChar(1000)') Id,
        COALESCE(
          C.query('..').value('(Question/questionId)[1]', 'NVarChar(1000)'),
          C.query('..').value('(SubQuestion/questionId)[1]', 'NVarChar(1000)')
        ) ParentId,
        C.value('questionDescription[1]', 'NVarChar(1000)') Description,
        NULLIF(C.value('questionHeader[1]', 'NVarChar(1000)'), '') Header,
        NULLIF(C.value('questionLabel[1]', 'NVarChar(1000)'), '') Label,
        C.value('version[1]', 'NVarChar(1000)') Version
    FROM
      @X.nodes('.//*[local-name(.)="Question" or local-name(.)="SubQuestion"]') X(C);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple xml document that looks like the following snippet. I need
I have an XML file formatted like this: <?xml version=1.0 encoding=utf-8?> <Snippets> <Snippet name=abc>
I have an xml document that looks like this <?xml version=1.0?> <XML> <VIDEO> <WIDTH>800</WIDTH>
I have an XML document: var xml:XML = new XML(<rootNode> <head> <meta name=template content=Default
I have an XML document that I'm trying to style via CSS. A relevant
I have an XML document looking similar to this: <items> <item cat=1 owner=14>bla</item> <item
I have an xml document object that I need to convert into a string.
I have an xml document where an xml node with a particular name, say
I have a snippet of an XML file that looks like: <?xml version=1.0 encoding=utf-16?>
I have a snippet of XML that looks like <body> Some text.... <nodeA>....</nodeA> more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.