|
-
Aug 13th, 2015, 12:06 PM
#1
[RESOLVED] SQL Server 2008 R2 - Default XML Namespace
I would like to add a namespace to the root element. I've got this code
Code:
;WITH XMLNAMESPACES(default 'urn:blah')
select
'hello' as 'Element1',
'world' as 'Element2'
FOR XML RAW(''), ROOT('TEST'), ELEMENTS
which gives me the namespace at every node.
Code:
<TEST xmlns="urn:blah">
<Element1 xmlns="urn:blah">hello</Element1>
<Element2 xmlns="urn:blah">world</Element2>
</TEST>
I've also tried adding the namespace through the xml modify method
Code:
declare @result xml = (
select
'hello' as 'Element1',
'world' as 'Element2'
FOR XML RAW(''), ROOT('TEST'), ELEMENTS)
SET @result.modify('insert attribute xmlns {"urn:x12:schemas:005:010:834A1A1:BenefitEnrollmentAndMaintenance"} into (/*)[1]')
but that gives me an error
XQuery [modify()]: Cannot use 'xmlns' in the name expression of computed attribute constructor.
Any ideas? I REALLY would like to not implement string manipulation.
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Aug 13th, 2015, 12:27 PM
#2
Re: SQL Server 2008 R2 - Default XML Namespace
I also tried this which adds blank namespaces to the children, which is no good.
Code:
declare @result xml = (
select
'hello' as 'Element1',
'world' as 'Element2'
FOR XML RAW(''), ELEMENTS)
declare @root xml;
SET @root = '<TEST xmlns="urn:blah" />'
SET @root.modify('insert sql:variable("@result") into (/*)[1]')
select @root;
Code:
<TEST xmlns="urn:blah">
<Element1 xmlns="">hello</Element1>
<Element2 xmlns="">world</Element2>
</TEST>
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Aug 13th, 2015, 12:53 PM
#3
Re: SQL Server 2008 R2 - Default XML Namespace
At first I was trying to figure out what the end result what... then it clicked... what you're after is this as the XML:
Code:
<TEST xmlns="urn:blah">
<urn:Element1>hello</urn:Element1>
<urn:Element2>world</urn:Element2>
</TEST>
Right?
LEt me dig through some of my stuff... I think I've done this before...
-tg
-
Aug 13th, 2015, 12:58 PM
#4
Re: SQL Server 2008 R2 - Default XML Namespace
actually... this turned out to be simple...
https://msdn.microsoft.com/en-us/library/ms177400.aspx
here's what I came up with:
Code:
;WITH XMLNAMESPACES('blah' as urn)
select
'hello' as 'urn:Element1',
'world' as 'urn:Element2'
FOR XML RAW('TEST'), ELEMENTS
and the result is:
Code:
<TEST xmlns:urn="blah">
<urn:Element1>hello</urn:Element1>
<urn:Element2>world</urn:Element2>
</TEST>
-tg
-
Aug 13th, 2015, 09:24 PM
#5
Re: SQL Server 2008 R2 - Default XML Namespace
 Originally Posted by techgnome
At first I was trying to figure out what the end result what... then it clicked... what you're after is this as the XML:
Code:
<TEST xmlns="urn:blah">
<urn:Element1>hello</urn:Element1>
<urn:Element2>world</urn:Element2>
</TEST>
Right?
LEt me dig through some of my stuff... I think I've done this before...
-tg
Close . . .
Code:
<TEST xmlns="urn:blah">
<Element1>hello</Element1>
<Element2>world</Element2>
</TEST>
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Aug 17th, 2015, 03:54 PM
#6
Re: SQL Server 2008 R2 - Default XML Namespace
I ended up using the first example I posted. I think it is overkill to list the namespace in the child nodes, but the .NET classes I built to deserialize the XML didn't mind.
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|