title: XML element name from data in Postgres
body: <p>I have data in a table like this:</p>
  
  <pre><code>ID  Date        Side      Qty
  1   2015-07-01  buy       1000
  2   2015-07-02  buy       1000
  3   2015-07-03  sell      1000
  4   2015-07-04  sell      1000
  </code></pre>
  
  <p>I need to extract it as XML in a format like this:</p>
  
  <pre><code>&lt;trades&gt;
      &lt;buy&gt;
          &lt;date&gt;2015-07-01&lt;/date&gt;
          &lt;qty&gt;1000&lt;/qty&gt;
      &lt;/buy&gt;
      ...
      &lt;sell&gt;
          &lt;date&gt;2015-07-03&lt;/date&gt;
          &lt;qty&gt;1000&lt;/qty&gt;      
      &lt;/sell&gt;
      ...
  &lt;/trades&gt;
  </code></pre>
  
  <p>The data format I am working to specifies that each  child entry has to be contained in a tag <code>&lt;buy&gt;</code> or <code>&lt;sell&gt;</code> which means that the name of the tag has to be generated from the "Side" column.  The trouble is I don't know how to set the name of an xml element from data or even if this is possible. This is my code:</p>
  
  <pre><code>select 
  xmlelement(
          name "trades",
          xmlagg(  
          xmlelement(name Side
              xmlconcat(
                  xmlelement(name "date", Date), ...
  </code></pre>
  
  <p>This generates </p>
  
  <pre><code>&lt;trades&gt;
      &lt;Side&gt;
          &lt;date&gt;2015-07-01&lt;/date&gt;
          &lt;qty&gt;1000&lt;/qty&gt;
      &lt;/Side&gt;
      ...
      &lt;Side&gt;
          &lt;date&gt;2015-07-03&lt;/date&gt;
          &lt;qty&gt;1000&lt;/qty&gt;      
      &lt;/Side&gt;
      ...
  &lt;/trades&gt;
  </code></pre>
  
  <p>In other words, the word "Side" gets used instead of the value of the column.  Is it possible to use data values as xml element names in Postgres?</p>
  
  <p>I'm using 9.4</p>