【问题标题】:How to add namespaces to root node in SQL Server 2005 using XML FOR PATH如何使用 XML FOR PATH 将命名空间添加到 SQL Server 2005 中的根节点
【发布时间】:2012-06-20 21:30:24
【问题描述】:
我使用 SQL Server 2005 创建了 Select...FOR XML PATH 语句,并且需要向根节点添加多个命名空间和架构引用。
xsi:schemaLocation="http://somewhere/v1/ schemaname.xsd"
xmlns="http://somewhere/v1/"
xmlns:lom="http://somewhere/lom/v1/"
xmlns:a="http://somewhere/a/v1/"
xmlns:cf="http://somewhere/cf/v1/"
xmlns:co="http://somewhere/co/v1/"
xmlns:hx="http://somewhere/hx/v1/"
xmlns:m="http://somewhere/m/v1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
任何指针将不胜感激。
谢谢。
【问题讨论】:
标签:
sql-server-2005
xml-namespaces
for-xml-path
【解决方案1】:
你可以试试这样的:
with xmlnamespaces
(
default 'http://somewhere/v1/',
'http://somewhere/lom/v1/' as lom,
'http://somewhere/a/v1/' as a,
'http://somewhere/cf/v1/' as cf,
'http://somewhere/co/v1/' as co,
'http://somewhere/hx/v1/' as hx,
'http://somewhere/m/v1/' as m,
'http://www.w3.org/2001/XMLSchema-instance' as xsi
)
select 'http://somewhere/v1/ schemaname.xsd' as "@xsi:schemaLocation"
for xml path('element')
结果:
<element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="http://somewhere/m/v1/"
xmlns:hx="http://somewhere/hx/v1/"
xmlns:co="http://somewhere/co/v1/"
xmlns:cf="http://somewhere/cf/v1/"
xmlns:a="http://somewhere/a/v1/"
xmlns:lom="http://somewhere/lom/v1/"
xmlns="http://somewhere/v1/"
xsi:schemaLocation="http://somewhere/v1/ schemaname.xsd" />