How can I position the element generated by XMLTEXT in a FOR XML query? I'm trying to use FOR XML to simulate an OData feed. One of the requirements I have is that somewhere I need an XML element which is constant for all records; I was planning to use the XMLTEXT directive for this.

My XML needs to look like this; for every record in the database,

&lt;entry&gt;
    &lt;id&gt;1&lt;/id&gt;
    &lt;test type="dummy" /&gt;
    &lt;!-- some more nested elements --&gt;
&lt;/entry&gt;


When I use the following query, the &lt;test&gt; element appears before the &lt;id&gt; element, even though the declaration appears after it:

CREATE TABLE #temp (id int);
INSERT INTO #temp VALUES (1), (2);

SELECT
  1    AS Tag,
  NULL AS Parent,
  id   AS [entry!1!id!ELEMENT],
  '&lt;test type="dummy" /&gt;'
       AS [entry!1!test!XMLTEXT]
  -- other columns
  FROM #temp
  FOR XML EXPLICIT;

DROP TABLE #temp;


This gives the following results (fiddle):

&lt;entry&gt;
    &lt;test type="dummy"/&gt;
    &lt;id&gt;1&lt;/id&gt;
&lt;/entry&gt;


Am I doing something wrong here, or is this a limitation of XMLTEXT? A workaround might be to treat it as a regular nested element, but would that work even though I have more nested elements further down in the &lt;entry&gt; element?
After some more fiddling, I found out that there is an XML directive which does the job (if I omit the attribute name, test): 

SELECT
  1    AS Tag,
  NULL AS Parent,
  id   AS [entry!1!id!ELEMENT],
  '&lt;test type="dummy" /&gt;'
       AS [entry!1!!XML]
  FROM #temp
  FOR XML EXPLICIT;


This produces the results I want:

&lt;entry&gt;&lt;id&gt;1&lt;/id&gt;&lt;test type="dummy" /&gt;&lt;/entry&gt;


(demo)

This directive is mentioned briefly here but I couldn't find more documentation or any examples.