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 6176901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:10:54+00:00 2026-05-24T00:10:54+00:00

I have some (untyped) XML being stored in SQL Server 2005 that I need

  • 0

I have some (untyped) XML being stored in SQL Server 2005 that I need to transform into a normalized structure. The structure of the document currently looks like so:

<wrapper>
 <parent />
 <node />
 <node />
 <node />

 <parent />
 <node />
 <node />
 <node />
<wrapper> 

I want to transform it to look like this:

<wrapper>
 <parent>
  <node />
  <node />
  <node />
 </parent>
 <parent>
  <node />
  <node />
  <node />
 </parent>
<wrapper> 

I can select the XML out into a relational structure if I need to, put the problem is there are no attributes linking the parent and the child nodes together, so order becomes an issue when using set-based operations. How can I use the .nodes()/.value()/other SQL Server XML APIs to transform this data? The transformation needs to run as part of a batch SQL script so extracting it into another tool/language is not a reasonable option for me.

  • 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-24T00:10:55+00:00Added an answer on May 24, 2026 at 12:10 am

    Actually – following code works (grouping here may be isn’t very optimal, but anyway):

    declare @xml xml = '
        <wrapper>
         <parent id="1" />
         <node id="1" />
         <node id="2" />
         <node id="3" />
    
         <parent id="2" />
         <node id="4" />
         <node id="5" />
         <node id="6" />
        </wrapper> 
    '
    
    ;with px as
    (
        select row_number() over (order by (select 1)) as RowNumber
            ,t.v.value('@id', 'int') as Id
            ,t.v.value('local-name(.)', 'nvarchar(max)') as TagName
        from @xml.nodes('//wrapper/*') as t(v)
    )
    select p.Id as [@id],
        (
            select n.Id as id
            from px n
            where n.TagName = 'node'
                and n.RowNumber > p.RowNumber
                and not exists
                (
                    select null
                    from px np
                    where np.TagName = 'parent'
                        and np.RowNumber > p.RowNumber
                        and np.RowNumber < n.RowNumber
                )
            order by n.RowNumber
            for xml raw('node'), type
        )
    from px p
    where p.TagName = 'parent'
    order by p.RowNumber
    for xml path('parent'), root('wrapper')

    But I don’t recommend to use it. See here: http://msdn.microsoft.com/en-us/library/ms172038%28v=sql.90%29.aspx:

    In SQLXML 4.0, document order is not always determined

    So I’m not sure that we can rely on order of tags inside wrapper (and code above is more just for fun than for practical use).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some UI in VB 2005 that looks great in XP Style, but
I have a query that strips data from a XML string, inserts it into
I have to call some Java library code that returns an untyped java.util.List and
I have some XML and need to be able to read the data within.
I have some Java code that takes an XML (SOAP) message and returns the
I have some server code that is generating thumbnails when an image is uploaded.
I have some tables that has the following structure: cat( cat_id(pk), cat_name) ven( ven_id(pk),
We have some input data that sometimes appears with &nbsp characters on the end.
We have some files on our website that users of our software can download.
I have for some time build n-tier Applications using a database server as the

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.