title: XSL & XPath - Select Element Name of Attribute
body: <p>I have a rather specific question perhaps..</p>
  
  <p>I have a user that will enter in some criteria on a form, and then it builds an XPath from their input. Let's say their input creates the following: </p>
  
  <pre><code>//*[@color='red']/@*
  </code></pre>
  
  <p>Which essentially means they want to see all attributes of any element with the @color = red.</p>
  
  <p>I can display exactly what the expression asks for (all attributes), but I wanted to add information that could be useful, such as the element's name the attributes belong to.</p>
  
  <p>One option would be to add to the XPath to also display the elements name: <em>(a shorter way to create this expression without the use of | would be nice to know.. so if you have suggestions, that would be awesome!)</em></p>
  
  <pre><code>//*[@color='red']/@* | //*[@color='red']
  </code></pre>
  
  <p>Another option (that I hope is possible) is to select the element name the attribute belongs to from the template without altering the XPath expression (because I might want to use this expression in another context without the element name).</p>
  
  <p>I <strong><em>want</em></strong> to have this XSLT:</p>
  
  <pre><code>&lt;xsl:param name="built_expression" select="//*[@color='red']/@* /&gt;
  &lt;xsl:template match="/"&gt;
      &lt;html&gt;
          &lt;body&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Element Name&lt;/th&gt;
                      &lt;th&gt;Element Content&lt;/th&gt;
                  &lt;/tr&gt;
  
                  &lt;xsl:apply-templates select="$built_expression"/&gt;
              &lt;/table&gt;
          &lt;/body&gt;
      &lt;/html&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="@*|node()"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;xsl:value-of select="ELEMENT name()" /&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;xsl:value-of select="node()" /&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
  
      ... other display stuff to do with the attributes ...
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p><strong><em>This obviously doesn't work... but I hope you get my point..</em></strong></p>
  
  <pre><code>&lt;xsl:value-of select="name()"/&gt;
  </code></pre>
  
  <p><strong><em>just returns the name of the attribute, but I need to select the name of the attribute's element.</em></strong></p>
  
  <p>Thanks! Let me know if I need to clarify anything!</p>