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

  • SEARCH
  • Home
  • 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 8877859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:34:14+00:00 2026-06-14T19:34:14+00:00

I am assuming the answer to my question is going to be something simple

  • 0

I am assuming the answer to my question is going to be something simple which I can’t figure out myself. Here is the scenario:

I am using SQL Server 2008 R2 where a table has an XML column where the data is saved in the following format:

<Person>
    <firstName>John</firstName>
    <lastName>Lewis</lastName>
</Person>

The Person node can have any number of child-nodes for which the element names might be different (not known beforehand). I am looking for a query to return an XML which has the values for all the nodes as attributes.

So the output for the above XML should be:

<Person firstName="John" lastName="Lewis"/>

I can’t think of a query to get the above output. I don’t want to use a query like

Select 
      PersonColumn.value('(/Person/firstName)[1]', 'varchar(100)') AS '@firstName'
    , PersonColumn.value('(/Person/lastName)[1]', 'varchar(100)') AS '@lastName'
FROM MyTable
WHERE MyTable.MyPrimaryKey=1
FOR XML PATH('Person'), TYPE

since I don’t know what nodes might there be under the Person node.

  • 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-06-14T19:34:15+00:00Added an answer on June 14, 2026 at 7:34 pm

    I’ve tried to do this

    select
        PersonColumn.query('
            element Person {
                for $i in /Person/*
                return attribute {local-name($i)} {string($i)}
            }
        ')
    from MyTable
    

    but it turns out that it’s impossible to use dynamic attribute names

    XQuery [MyTable.PersonColumn.query()]: Only constant expressions are supported for the name expression of computed element and attribute constructors.: select PersonColumn.query(' element Person { for $i in /Person/* return attribute {local-name($i)} {string($i)} } ') from MyTable
    

    So best I can do so far is

    select 
        cast(
            '<Person ' + 
            (
                select
                    PersonColumn.query('
                    for $i in /Person/*
                    return concat(local-name($i), "=""", data($i), """")
                    ').value('.', 'nvarchar(max)')
                for xml path('')
            ) + '/>'
        as xml)
    from MyTable
    

    It’s also possible to do this

    select
        cast(
            '<Person ' + 
            PersonColumn.query('
                for $i in /Person/*
                return concat(local-name($i), "=""", data($i), """")
            ').value('.', 'nvarchar(max)') +
            '/>'
          as xml)
    from MyTable
    

    but it will not work if your data contains some characters like < > and so on

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

Sidebar

Related Questions

This is a pretty simple question and I'm assuming the answer is It doesn't
I have a question that's so simple I cannot believe I can't answer it
I'm looking for an extended answer to the question asked here: Determine Whether Two
Okay, so the answer to my question might not be the problem but here's
Being quite new at JavaScript, I'm assuming the answer to my question is extremely
I've looked through the site and can't find an answer to my question. I'm
I've read every question and answer on this topic I can find on this
Very simple question, hoping for a very simple answer. I've been looking at a
I'm assuming the answer to this question is that it's impossible, but I'm asking
I have what I think is probably quite a simple question to answer (for

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.