Results 1 to 6 of 6

Thread: [RESOLVED] SQL Server 2008 R2 - Default XML Namespace

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Resolved [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

  2. #2

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: SQL Server 2008 R2 - Default XML Namespace

    Quote Originally Posted by techgnome View Post
    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

  6. #6

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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
  •  



Click Here to Expand Forum to Full Width