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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:34:18+00:00 2026-05-17T01:34:18+00:00

I try to add a namespace on xml using WITH XMLNAMESPACES. When I execute

  • 0

I try to add a namespace on xml using WITH XMLNAMESPACES.

When I execute my queries, the namespace is added with the root element but with the second element I have xmlns=”” as well… and I would like to remove that…

I provided an example:

Queries for creating the table and the data:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblTest](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](30) NOT NULL,
 CONSTRAINT [PK_tblTest] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tblTest] ON
INSERT [dbo].[tblTest] ([Id], [Name]) VALUES (1, N'Barack')
INSERT [dbo].[tblTest] ([Id], [Name]) VALUES (2, N'Nicolas')
INSERT [dbo].[tblTest] ([Id], [Name]) VALUES (3, N'Brian')
SET IDENTITY_INSERT [dbo].[tblTest] OFF

I generate the xml with these queries:

DECLARE @Xml xml
SET @Xml = (SELECT Id, Name
            FROM   dbo.tblTest 
            FOR XML PATH('Row'), ROOT('DataRows'));

WITH XMLNAMESPACES (DEFAULT 'http://www.mynamespace.com')
SELECT  @Xml FOR XML PATH('Names');

Xml generated:

<Names xmlns="http://www.mynamespace.com">
  <DataRows xmlns="">
    <Row>
      <Id>1</Id>
      <Name>Barak</Name>
    </Row>
    <Row>
      <Id>2</Id>
      <Name>Nicolas</Name>
    </Row>
    <Row>
      <Id>3</Id>
      <Name>Brian</Name>
    </Row>
  </DataRows>
</Names>

So, I try this as well:

DECLARE @Xml xml

;WITH XMLNAMESPACES (DEFAULT 'http://www.mynamespace.com')
SELECT @Xml = (SELECT Id, Name
    FROM   dbo.tblTest 
    FOR XML PATH('Row'), TYPE);

;WITH XMLNAMESPACES (DEFAULT 'http://www.mynamespace.com')
SELECT @Xml
FOR XML PATH('DataRows'), ROOT('Names')

the xml generated is now:

<Names xmlns="http://www.mynamespace.com">
  <DataRows>
    <Row xmlns="http://www.mynamespace.com">
      <Id>1</Id>
      <Name>Barak</Name>
    </Row>
    <Row xmlns="http://www.mynamespace.com">
      <Id>2</Id>
      <Name>Nicolas</Name>
    </Row>
    <Row xmlns="http://www.mynamespace.com">
      <Id>3</Id>
      <Name>Brian</Name>
    </Row>
  </DataRows>
</Names>
  • 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-17T01:34:19+00:00Added an answer on May 17, 2026 at 1:34 am

    Daniel, the xmlns="" on the <DataRows> element means, set the default namespace for <DataRows> and all descendants to no namespace.

    In other words, if the xmlns="" were not there, the whole XML tree would be in the http://www.mynamespace.com namespace. (Because namespace declarations are inherited, until overridden.) And that’s probably what you wanted. But SQL Server thinks you wanted only the <Names> element to be in that namespace. So it is “helpfully” removing the default namespace for all descendant elements.

    The solution, then, is to tell SQL Server that all the elements, not just <Names>, should be in the http://www.mynamespace.com namespace.

    (If you ask me how to do that, the answer is I don’t know SQL Server XML features that well. But maybe clarifying what’s happening and what needs to happen will help you figure out how to make it happen.)

    Update in light of newly posted query and output:

    @Daniel, your output is now technically correct. All the output elements are in the http://www.mynamespace.com namespace. The xmlns="http://www.mynamespace.com" declarations on the <Row> elements are redundant… they don’t change the namespace of any element.

    You may not like them the extra declarations, but they should not make any difference to any downstream XML tools.

    If you want to remove them, and if you can’t do that by tweaking the SQL query, you could run the resulting XML through an XSLT stylesheet. Even an identity transformation will probably get rid of the redundant namespace declarations, I believe.

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

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's actually a 2D barcode, and it is a QR… May 17, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer Absolutely not, and your example demonstrates why. You've introduced a… May 17, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer To answer my own question: The easiest solution was to… May 17, 2026 at 2:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm using VB 2008 and I'm trying to add a xmlns=mynamespace attribute to an
There is a schema with the following boring root element: <?xml version=1.0 encoding=utf-8?> <xs:schema
Lets say I have the following XML file: <?xml version=1.0 encoding=utf-8?> <Customers xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation=Customer.xsd>
I have an XSLT transform I am using to process an XML file, inserting
I have an XML document that looks like this: <kmsg xmlns=http://url1 xmlns:env=url1 xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:schemaLocation=http://location
#include<iostream> using namespace std; class base { public: virtual void add() { cout <<
I am creating XML using Linq to XML and C#. It all works great
I do not know enough about the XML namespace to see my error. Do
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
1 namespace Uploader 2 { 3 using System; 4 using System.IO; 5 using System.ServiceModel;

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.