Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I have understood correctly, you are referring to the behavior that you might see in a query like this: </p> <pre><code>DECLARE @Order TABLE ( OrderID INT, OrderDate DATETIME) DECLARE @OrderDetail TABLE ( OrderID INT, ItemID VARCHAR(1), ItemName VARCHAR(50), Qty INT) INSERT @Order VALUES (1, '2010-01-01'), (2, '2010-01-02') INSERT @OrderDetail VALUES (1, 'A', 'Drink', 5), (1, 'B', 'Cup', 2), (2, 'A', 'Drink', 2), (2, 'C', 'Straw', 1), (2, 'D', 'Napkin', 1) ;WITH XMLNAMESPACES('http://test.com/order' AS od) SELECT OrderID AS "@OrderID", (SELECT ItemID AS "@od:ItemID", ItemName AS "data()" FROM @OrderDetail WHERE OrderID = o.OrderID FOR XML PATH ('od.Item'), TYPE) FROM @Order o FOR XML PATH ('od.Order'), TYPE, ROOT('xml') </code></pre> <p>Which gives the following results: </p> <pre><code>&lt;xml xmlns:od="http://test.com/order"&gt; &lt;od.Order OrderID="1"&gt; &lt;od.Item xmlns:od="http://test.com/order" od:ItemID="A"&gt;Drink&lt;/od.Item&gt; &lt;od.Item xmlns:od="http://test.com/order" od:ItemID="B"&gt;Cup&lt;/od.Item&gt; &lt;/od.Order&gt; &lt;od.Order OrderID="2"&gt; &lt;od.Item xmlns:od="http://test.com/order" od:ItemID="A"&gt;Drink&lt;/od.Item&gt; &lt;od.Item xmlns:od="http://test.com/order" od:ItemID="C"&gt;Straw&lt;/od.Item&gt; &lt;od.Item xmlns:od="http://test.com/order" od:ItemID="D"&gt;Napkin&lt;/od.Item&gt; &lt;/od.Order&gt; &lt;/xml&gt; </code></pre> <p>As you said, the namespace is repeated in the results of the subqueries.</p> <p>This behavior is a feature according to a conversation on devnetnewsgroup (website now defunct) although there is the option to <a href="https://connect.microsoft.com/SQLServer/feedback/details/265956" rel="nofollow noreferrer">vote</a> on changing it.</p> <p>My proposed solution is to revert back to <code>FOR XML EXPLICIT</code>:</p> <pre><code>SELECT 1 AS Tag, NULL AS Parent, 'http://test.com/order' AS [xml!1!xmlns:od], NULL AS [od:Order!2], NULL AS [od:Order!2!OrderID], NULL AS [od:Item!3], NULL AS [od:Item!3!ItemID] UNION ALL SELECT 2 AS Tag, 1 AS Parent, 'http://test.com/order' AS [xml!1!xmlns:od], NULL AS [od:Order!2], OrderID AS [od:Order!2!OrderID], NULL AS [od:Item!3], NULL [od:Item!3!ItemID] FROM @Order UNION ALL SELECT 3 AS Tag, 2 AS Parent, 'http://test.com/order' AS [xml!1!xmlns:od], NULL AS [od:Order!2], o.OrderID AS [od:Order!2!OrderID], d.ItemName AS [od:Item!3], d.ItemID AS [od:Item!3!ItemID] FROM @Order o INNER JOIN @OrderDetail d ON o.OrderID = d.OrderID ORDER BY [od:Order!2!OrderID], [od:Item!3!ItemID] FOR XML EXPLICIT </code></pre> <p>And see these results: </p> <pre><code>&lt;xml xmlns:od="http://test.com/order"&gt; &lt;od:Order OrderID="1"&gt; &lt;od:Item ItemID="A"&gt;Drink&lt;/od:Item&gt; &lt;od:Item ItemID="B"&gt;Cup&lt;/od:Item&gt; &lt;/od:Order&gt; &lt;od:Order OrderID="2"&gt; &lt;od:Item ItemID="A"&gt;Drink&lt;/od:Item&gt; &lt;od:Item ItemID="C"&gt;Straw&lt;/od:Item&gt; &lt;od:Item ItemID="D"&gt;Napkin&lt;/od:Item&gt; &lt;/od:Order&gt; &lt;/xml&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload