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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:46:02+00:00 2026-05-15T17:46:02+00:00

UPDATE : I’ve discovered there is a Microsoft Connect item raised for this issue

  • 0

UPDATE: I’ve discovered there is a Microsoft Connect item raised for this issue here

When using FOR XML PATH and WITH XMLNAMESPACES to declare a default namespace, I will get the namespace declaration duplicated in any top level nodes for nested queries that use FOR XML, I’ve stumbled across a few solutions on-line, but I’m not totally convinced…

Here’s an Complete Example

/*
drop table t1
drop table t2
*/
create table t1 ( c1 int, c2 varchar(50))
create table t2 ( c1 int, c2 int, c3 varchar(50))
insert t1 values 
(1, 'Mouse'),
(2, 'Chicken'),
(3, 'Snake');
insert t2 values
(1, 1, 'Front Right'),
(2, 1, 'Front Left'),
(3, 1, 'Back Right'),
(4, 1, 'Back Left'),
(5, 2, 'Right'),
(6, 2, 'Left')



;with XmlNamespaces( default 'uri:animal')
select 
    a.c2 as "@species"
    , (select l.c3 as "text()" 
       from t2 l where l.c2 = a.c1 
       for xml path('leg'), type) as "legs"
from t1 a
for xml path('animal'), root('zoo')

What’s the best solution?

  • 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-15T17:46:03+00:00Added an answer on May 15, 2026 at 5:46 pm

    If I have understood correctly, you are referring to the behavior that you might see in a query like this:

    DECLARE @Order TABLE (
      OrderID INT, 
      OrderDate DATETIME)
    
    DECLARE @OrderDetail TABLE (
      OrderID INT, 
      ItemID VARCHAR(1), 
      ItemName VARCHAR(50), 
      Qty INT)
    
    INSERT @Order 
    VALUES 
    (1, '2010-01-01'),
    (2, '2010-01-02')
    
    INSERT @OrderDetail 
    VALUES 
    (1, 'A', 'Drink',  5),
    (1, 'B', 'Cup',    2),
    (2, 'A', 'Drink',  2),
    (2, 'C', 'Straw',  1),
    (2, 'D', 'Napkin', 1)
    
    ;WITH XMLNAMESPACES('http://test.com/order' AS od) 
    SELECT
      OrderID AS "@OrderID",
      (SELECT 
         ItemID AS "@od:ItemID", 
         ItemName AS "data()" 
       FROM @OrderDetail 
       WHERE OrderID = o.OrderID 
       FOR XML PATH ('od.Item'), TYPE)
    FROM @Order o 
    FOR XML PATH ('od.Order'), TYPE, ROOT('xml')
    

    Which gives the following results:

    <xml xmlns:od="http://test.com/order">
      <od.Order OrderID="1">
        <od.Item xmlns:od="http://test.com/order" od:ItemID="A">Drink</od.Item>
        <od.Item xmlns:od="http://test.com/order" od:ItemID="B">Cup</od.Item>
      </od.Order>
      <od.Order OrderID="2">
        <od.Item xmlns:od="http://test.com/order" od:ItemID="A">Drink</od.Item>
        <od.Item xmlns:od="http://test.com/order" od:ItemID="C">Straw</od.Item>
        <od.Item xmlns:od="http://test.com/order" od:ItemID="D">Napkin</od.Item>
      </od.Order>
    </xml>
    

    As you said, the namespace is repeated in the results of the subqueries.

    This behavior is a feature according to a conversation on devnetnewsgroup (website now defunct) although there is the option to vote on changing it.

    My proposed solution is to revert back to FOR XML EXPLICIT:

    SELECT
      1 AS Tag,
      NULL AS Parent,
      'http://test.com/order' AS [xml!1!xmlns:od],
      NULL AS [od:Order!2],
      NULL AS [od:Order!2!OrderID],
      NULL AS [od:Item!3],
      NULL AS [od:Item!3!ItemID]
    UNION ALL
    SELECT 
      2 AS Tag,
      1 AS Parent,
      'http://test.com/order' AS [xml!1!xmlns:od],
      NULL AS [od:Order!2],
      OrderID AS [od:Order!2!OrderID],
      NULL AS [od:Item!3],
      NULL [od:Item!3!ItemID]
    FROM @Order 
    UNION ALL
    SELECT
      3 AS Tag,
      2 AS Parent,
      'http://test.com/order' AS [xml!1!xmlns:od],
      NULL AS [od:Order!2],
      o.OrderID AS [od:Order!2!OrderID],
      d.ItemName AS [od:Item!3],
      d.ItemID AS [od:Item!3!ItemID]
    FROM @Order o INNER JOIN @OrderDetail d ON o.OrderID = d.OrderID
    ORDER BY [od:Order!2!OrderID], [od:Item!3!ItemID]
    FOR XML EXPLICIT
    

    And see these results:

    <xml xmlns:od="http://test.com/order">
      <od:Order OrderID="1">
        <od:Item ItemID="A">Drink</od:Item>
        <od:Item ItemID="B">Cup</od:Item>
      </od:Order>
      <od:Order OrderID="2">
        <od:Item ItemID="A">Drink</od:Item>
        <od:Item ItemID="C">Straw</od:Item>
        <od:Item ItemID="D">Napkin</od:Item>
      </od:Order>
    </xml>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to
UPDATE - A comprehensive comparison, updated as of February 2015, can be found here:
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
Update: Please read this question in the context of design principles, elegance, expression of
Update: Solved, with code I got it working, see my answer below for the
Update: giving a much more thorough example. The first two solutions offered were right
Update : Looks like the query does not throw any timeout. The connection is
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add

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.