title: XSLT: Select an element by variable value
body: <p>I have some XML structured like below: </p>
  
  <pre><code>&lt;report&gt;
    &lt;columns&gt;
      &lt;column col_id="col_1"&gt;Column 1 title&lt;/column&gt;
      &lt;column col_id="col_2"&gt;Column 2 title&lt;/column&gt;
      &lt;column col_id="col_3"&gt;Column 3 title&lt;/column&gt;
    &lt;/columns&gt;
    &lt;rows&gt;
      &lt;row&gt;
        &lt;col_1&gt;Value 1&lt;/col_1&gt;
        &lt;!-- No col_2 value exists --&gt;
        &lt;col_3&gt;value 3&lt;/col_3&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col_1&gt;Value 1&lt;/col_1&gt;
        &lt;col_2&gt;Value 2&lt;/col_2&gt;
        &lt;col_3&gt;value 3&lt;/col_3&gt;
        &lt;col_4&gt;Value 4&lt;/col_4&gt;&lt;!-- Not interested in this value --&gt;
      &lt;/row&gt;
    &lt;/rows&gt;
  &lt;/report&gt;
  </code></pre>
  
  <p>I want to output this data as a HTML table using XSLT.  </p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
     &lt;xsl:output method="html" encoding="utf-8" indent="yes" /&gt;
     &lt;xsl:template match="Report"&gt;
        &lt;!-- Get the column names --&gt;
        &lt;xsl:variable name="arr_columns" select="./columns/*" /&gt;
  
        &lt;div id="example_report" class="report"&gt;
           &lt;table style="width:100%"&gt;
              &lt;thead&gt;
                 &lt;tr class="header"&gt;
                    &lt;!-- Columns first --&gt;
                    &lt;xsl:for-each select="$arr_columns"&gt;
                       &lt;th&gt;&lt;xsl:value-of select="." /&gt;&lt;/th&gt;
                    &lt;/xsl:for-each&gt;
                 &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                 &lt;!-- Now the actual data --&gt;
                 &lt;xsl:for-each select="./rows/row"&gt;
                    &lt;xsl:variable name="row" select="." /&gt;
                    &lt;tr&gt;
                       &lt;xsl:for-each select="$arr_columns"&gt;
                          &lt;xsl:variable name="col_id" select="./@column_id" /&gt;
                          &lt;td&gt;&lt;xsl:value-of select="$row/$col_id" /&gt;&lt;/td&gt;
                       &lt;/xsl:for-each&gt;
                    &lt;/tr&gt;
                 &lt;/xsl:for-each&gt;
              &lt;/tbody&gt;
           &lt;/table&gt;
        &lt;/div&gt;
     &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>I was hoping that <code>&lt;xsl:value-of select="$row/$col_id" /&gt;</code> would let me select the column based on its name as stored in the col_id variable.  However, PHP's XSLTProcessor blows up on it instead and the report fails to render.  </p>
  
  <blockquote>
    <p>PHP Warning:  XSLTProcessor::transformToXml(): No stylesheet associated to this object</p>
  </blockquote>
  
  <p>I've tried outputting just $row (which as expected outputs the entire row as a string), and $row/col_1 (which outputs the contents of col_1 as expected), but trying to access a node based on the value of a variable just doesn't seem to work.  If I output the value of $col_id, it displays the name of the node I want to output, so that variable is getting correctly set in the loop.  </p>
  
  <p>I'm guessing that it is possible and I'm doing this wrong, but I'm having a hard time finding an example of how to do this correctly.  I did find examples that let you select nodes based on attributes, but as you can see the nodes I have don't have attributes, their name indicates what they hold.  </p>
  
  <p>Can someone help with writing the correct selector for this case?  </p>
  
  <p>For completeness, this is the output I'm hoping to generate : </p>
  
  <pre><code>&lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Column 1 title&lt;/th&gt;
        &lt;th&gt;Column 2 title&lt;/th&gt;
        &lt;th&gt;Column 3 title&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;Value 1&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;value 3&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;Value 1&lt;/td&gt;
        &lt;td&gt;value 2&lt;/td&gt;
        &lt;td&gt;value 3&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>