Monday, March 12, 2012

How to suppress namespace attributes in nested XML?

Hello,
I've got a query that dynamically generates an XML schema based on a
lookup table. Everything works fine, except that I would like to
suppress the generation of the namespace declarations on the individual
elements in xml type created by the sub-select.
Here's the code:
WITH XMLNAMESPACES ('http://tempuri.org/techFitCategories.xsd' AS tns,
DEFAULT
'http://www.w3.org/2001/XMLSchema')
SELECT 'techFitCategories' AS "@.id",
'http://tempuri.org/techFitCategories.xsd' AS
"@.targetNamespace",
'techFitCategory' AS "element/@.name",
'tns:category' AS "element/@.type",
'category' AS "simpleType/@.name",
'string' AS "simpleType/restriction/@.base",
(SELECT CategoryName AS "@.value"
FROM SkillCategories
FOR XML PATH('enumeration'), TYPE) AS
"simpleType/restriction"
FOR XML PATH('schema')
Here's the output:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd"
id="techFitCategories"targetNamespace="http://tempuri.org/techFitCategories.xsd">
<element name="techFitCategory" type="tns:category" />
<simpleType name="category">
<restriction base="string">
<enumeration xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="C#" />
<enumeration xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="Java" />
<enumeration xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="Linux" />
<enumeration xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="SQL Server
2005" />
<enumeration xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="VB.NET" />
<enumeration xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="WebSphere"
/>
<enumeration xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="Windows
2003" />
</restriction>
</simpleType>
</schema>
I'd like to get rid of the namespace declarations on the
<enumeration../> elements.
Any ideas?
Thanks in advance!
ca
One way is add a prefix to the default namespace. And add the prefix to
every nodes wherever you want. This is the way many experts recommend.
Pohwan Han. Seoul. Have a nice day.
<christopher.atkins@.e-gineering.com> wrote in message
news:1140685400.006495.6120@.p10g2000cwp.googlegrou ps.com...
> Hello,
> I've got a query that dynamically generates an XML schema based on a
> lookup table. Everything works fine, except that I would like to
> suppress the generation of the namespace declarations on the individual
> elements in xml type created by the sub-select.
>
> Here's the code:
> WITH XMLNAMESPACES ('http://tempuri.org/techFitCategories.xsd' AS tns,
> DEFAULT
> 'http://www.w3.org/2001/XMLSchema')
> SELECT 'techFitCategories' AS "@.id",
> 'http://tempuri.org/techFitCategories.xsd' AS
> "@.targetNamespace",
> 'techFitCategory' AS "element/@.name",
> 'tns:category' AS "element/@.type",
> 'category' AS "simpleType/@.name",
> 'string' AS "simpleType/restriction/@.base",
> (SELECT CategoryName AS "@.value"
> FROM SkillCategories
> FOR XML PATH('enumeration'), TYPE) AS
> "simpleType/restriction"
> FOR XML PATH('schema')
>
> Here's the output:
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd"
> id="techFitCategories"targetNamespace="http://tempuri.org/techFitCategories.xsd">
> <element name="techFitCategory" type="tns:category" />
> <simpleType name="category">
> <restriction base="string">
> <enumeration xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="C#" />
> <enumeration xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="Java" />
> <enumeration xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="Linux" />
> <enumeration xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="SQL Server
> 2005" />
> <enumeration xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="VB.NET" />
> <enumeration xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="WebSphere"
> />
> <enumeration xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://tempuri.org/techFitCategories.xsd" value="Windows
> 2003" />
> </restriction>
> </simpleType>
> </schema>
>
> I'd like to get rid of the namespace declarations on the
> <enumeration../> elements.
>
> Any ideas?
>
> Thanks in advance!
> ca
>
|||Pohwan,
Thank you kindly for your reply. If I parsed your response properly, I
don't believe this does what I'd hoped. It seems whenever you include
an XML column in a SELECT...FOR XML with the WITH NAMESPACES directive,
it will add the namespace declarations to each element of the XML.
I've tried using an xsd prefix on all the schema elements, but this
doesn't prevent it from adding the namespace attributes to the elements
in the nested XML typed column.
The only alternative I can think of is hacking a big nvarchar together:
not exactly the elegant solution I was hoping for. Don't get me wrong,
I've got a valid schema with the original query, but I'm expecting the
enumeration to get significantly large, and the result XML Schema
document would be huge--not a good thing.
If I've completely missed your point, do you think you could provide a
code example?
ca
|||You are right, Christopher. The prefix doesn't remove the namespace. It just
makes the namespace dormant. When prefixed namespaces are unused they remain
meaningless trash, while non-prefixed or default namespaces are part of
element names. Now all you need is ignoring the unused trash. Optionally you
can remove the namespace in some mid-tier such as XSLT.
Earlier I asked same question in beta SQL2005 group. About alternative of
exclude-result-prefixes of XSLT. And I got replied that there is none, IIRC,
by Michael Rys.
Pohwan Han. Seoul. Have a nice day.
"Christopher Atkins" <christopher.atkins@.e-gineering.com> wrote in message
news:1140721205.415683.28260@.u72g2000cwu.googlegro ups.com...
> Pohwan,
> Thank you kindly for your reply. If I parsed your response properly, I
> don't believe this does what I'd hoped. It seems whenever you include
> an XML column in a SELECT...FOR XML with the WITH NAMESPACES directive,
> it will add the namespace declarations to each element of the XML.
> I've tried using an xsd prefix on all the schema elements, but this
> doesn't prevent it from adding the namespace attributes to the elements
> in the nested XML typed column.
> The only alternative I can think of is hacking a big nvarchar together:
> not exactly the elegant solution I was hoping for. Don't get me wrong,
> I've got a valid schema with the original query, but I'm expecting the
> enumeration to get significantly large, and the result XML Schema
> document would be huge--not a good thing.
> If I've completely missed your point, do you think you could provide a
> code example?
> ca
>
|||We currently output all the namespaces that you provide and at every level
and there is no easy workaround. Note that the XML document is still
correct.
Best is if you file a feature request at
http://lab.msdn.microsoft.com/productfeedback/
by providing sample data, query current result and expected result.
Thanks
Michael
"Han" <hp4444@.kornet.net.korea> wrote in message
news:OP%23rjUQOGHA.3944@.tk2msftngp13.phx.gbl...
> You are right, Christopher. The prefix doesn't remove the namespace. It
> just makes the namespace dormant. When prefixed namespaces are unused they
> remain meaningless trash, while non-prefixed or default namespaces are
> part of element names. Now all you need is ignoring the unused trash.
> Optionally you can remove the namespace in some mid-tier such as XSLT.
> Earlier I asked same question in beta SQL2005 group. About alternative of
> exclude-result-prefixes of XSLT. And I got replied that there is none,
> IIRC, by Michael Rys.
> --
> Pohwan Han. Seoul. Have a nice day.
> "Christopher Atkins" <christopher.atkins@.e-gineering.com> wrote in message
> news:1140721205.415683.28260@.u72g2000cwu.googlegro ups.com...
>

No comments:

Post a Comment