Tôi muốn tạo một tệp sơ đồ trang web xml (bao gồm hình ảnh) trực tiếp từ cơ sở dữ liệu mà không cần quy trình khác (như chuyển đổi hoặc thủ thuật khác).Làm thế nào để tránh không gian tên trong các nút con bằng cách sử dụng FOR XML PATH?
truy vấn của tôi là:
;WITH XMLNAMESPACES(
DEFAULT 'http://www.sitemaps.org/schemas/sitemap/0.9',
'http://www.google.com/schemas/sitemap-image/1.1' as [image])
SELECT
(SELECT
'mysite' as [loc],
(select
'anotherloc'
as [image:loc]
for XML path('image:image'), type
)
for xml path('url'), type
)
for xml path('urlset'), type
Returns:
<urlset xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<loc>mysite</loc>
<image:image xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<image:loc>anotherloc</image:loc>
</image:image>
</url>
</urlset>
Nhưng tôi cần sản lượng này, mà không khai báo namespace lặp đi lặp lại:
<urlset xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>mysite</loc>
<image:image>
<image:loc>anotherloc</image:loc>
</image:image>
</url>
</urlset>
liên quan [trên DBA] (http://dba.stackexchange.com/questions/23624/how-to-remove-xmlns-from-child-elements -with-for-xml/23697 # 23697) –
Bản sao có thể có của [Làm cách nào để xóa không gian tên dự phòng trong truy vấn lồng nhau khi sử dụng FOR XML PATH] (http://stackoverflow.com/questions/3242070/how-do-i- xóa-thừa-không gian tên-trong-lồng nhau-truy vấn-khi-sử dụng-cho-xml-path) – Gabrielius