There is a solution for remove namespace here!, but I need it works in SQL function, I'm getting this error:
The FOR XML clause is invalid in views, inline functions, derived tables, and subqueries when they contain a set operator. To work around, wrap the SELECT containing a set operator using derived table syntax and apply FOR XML on top of it.
Can somebody help me?
Thanks Xtyan
From comment: This is the needed XML-output
<zoo xmlns:an="uri:animal">
<an:animal species="Mouse">
<an:legs>
<an:leg>Front Right</an:leg>
<an:leg>Front Left</an:leg>
<an:leg>Back Right</an:leg>
<an:leg>Back Left</an:leg>
</an:legs>
</an:animal>
<an:animal species="Chicken">
<an:legs>
<an:leg>Right</an:leg>
<an:leg>Left</an:leg>
</an:legs>
</an:animal>
<an:animal species="Snake" />
</zoo>
You will not like this probably...
I did not find a generic solution for this... It is no problem to create this with repeated namespaces. This is not wrong, but annoying:
One solution: build this via string-concatenation
But this is super-ugly...
an (almost) working solution...
The following solution uses
FLWOR
to re-create the XML after its creation, but this is not generic. It is necessary to add one element of this namespaces to the<zoo>
, otherwise the namespace is created on a deeper level repeatedly. I added the attributan:Dummy
.The result
Previous answer
Okay, I think I got this completely wrong in my first attempt. The following is an example, where a function returns the XML without added namespaces.
I use one of the later answers which builds the inner XML in advance without a namespace and creates the full XML as a second call with a namespace. Please check this out:
--the function
--the call
--clean up
The result