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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:04:52+00:00 2026-06-08T07:04:52+00:00

I am trying to match some XML output in a SQL Server 2008 TSQL

  • 0

I am trying to match some XML output in a SQL Server 2008 TSQL query. The XML I am matching can return Lists of items. The lists are identified by attribute name – not the node name.

The XML Path functions will return a wrapper node but I can’t find a way to add any attributes to that node. In my case they will be hard coded values based on the table name I am running they select against.

I have the ‘x’ as ‘x’ in there to keep the two lists separated. In my actual data that is not a problem as these are in different nodes. The problem is just how to get attributes added to the “List” node.

Here is a SQL Fiddle page for the example below:

Sample Schema

create table Table1 (Value varchar(50)); 
create table Table2 (Value varchar(50)); 

insert Table1 values
('A'), ('B'), ('C');

insert Table2 values
('X'), ('Y'), ('Z');

Sample Select

select 
(
 select Value as '@I'
 from Table1
 for XML PATH('L'), TYPE
) as List,
'x' as 'x', -- needed to keep the Lists apart.
(
 select Value as '@I'
 from Table2
 for XML PATH('L'), TYPE
) as List

for XML PATH

Actual Output

<row>
  <List>
    <L I="A"/>
    <L I="B"/>
    <L I="C"/>
  </List>

  <x>x</x>

  <List>
    <L I="X"/>
    <L I="Y"/>
    <L I="Z"/>
  </List>
</row>

Desired Output: (Adding the “Name” attribute to the list wrapper.)

<row>
  <List Name='Table1'>  <!-- Added Attribute "Name" here -->
    <L I="A"/>
    <L I="B"/>
    <L I="C"/>
  </List>

  <x>x</x>

  <List Name='Table2'>  <!-- Added Attribute "Name" here -->
    <L I="X"/>
    <L I="Y"/>
    <L I="Z"/>
  </List>
</row>
  • 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-08T07:04:53+00:00Added an answer on June 8, 2026 at 7:04 am

    This appears to give the correct output. An extra level of XML PATH nesting is required to get the attribute onto the List node:

    SELECT
    (SELECT 'Table1' AS '@name',
      (SELECT Value AS '@I'
       FROM Table1
       FOR XML PATH ('L'))
     FOR XML PATH('List'), TYPE)
    ,'x' AS'x'
    ,( SELECT 'Table2' AS '@name',
      (SELECT Value AS '@I'
       FROM Table2
       FOR XML PATH ('L'))
     FOR XML PATH('List'), TYPE
    )
    
    FOR XML PATH;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to transform some XML into HTML using XSLT . Problem: I can't
The pattern i am trying to match is some characters(letters+digits+hyphens (-) ) then 5
I'm trying to match variables in some PHP code which are missing their leading
I'm trying to get regexp to match some nested tags. (Yes I know I
I need some help trying to match a C include file with full path
I am trying to sort an xml using xsl. Got some sample from xml.com.
i'm trying to convert my log4j.properties into log4j.xml because i need to use some
I'm trying to canonicalize the representation of some XML data by sorting each element's
I'm trying to extract some information from an xml file using xslt. I've used
I'm trying to develop a schema that will validate some existing XML files I've

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.