title: How do I remove redundant namespace in nested query when using FOR XML PATH
body: <blockquote>
    <p><strong>UPDATE</strong>: I've discovered there is a Microsoft Connect item raised for this issue <a href="https://connect.microsoft.com/SQLServer/feedback/details/265956" rel="nofollow noreferrer">here</a></p>
  </blockquote>
  
  <p>When using <code>FOR XML PATH</code> and <code>WITH XMLNAMESPACES</code> to declare a default namespace, I will get the namespace declaration duplicated in any top level nodes for nested queries that use FOR XML, I've stumbled across a few solutions on-line, but I'm not totally convinced... </p>
  
  <p><strong>Here's an Complete Example</strong></p>
  
  <pre><code>/*
  drop table t1
  drop table t2
  */
  create table t1 ( c1 int, c2 varchar(50))
  create table t2 ( c1 int, c2 int, c3 varchar(50))
  insert t1 values 
  (1, 'Mouse'),
  (2, 'Chicken'),
  (3, 'Snake');
  insert t2 values
  (1, 1, 'Front Right'),
  (2, 1, 'Front Left'),
  (3, 1, 'Back Right'),
  (4, 1, 'Back Left'),
  (5, 2, 'Right'),
  (6, 2, 'Left')
  
  
  
  ;with XmlNamespaces( default 'uri:animal')
  select 
      a.c2 as "@species"
      , (select l.c3 as "text()" 
         from t2 l where l.c2 = a.c1 
         for xml path('leg'), type) as "legs"
  from t1 a
  for xml path('animal'), root('zoo')
  </code></pre>
  
  <p>What's the best solution?</p>