title: How to get a list of the distinct element names plus their attribute names using XSLT
body: <p>I would like to "scan" my xml files using xslt and get a list of the distinct element names <strong>as well as</strong> their attribute names.</p>
  
  <p>My XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;dictionary&gt;
      &lt;entry&gt;
          &lt;form type="hyperlemma" xml:lang="cu"&gt;
              &lt;note type="editor's comment"&gt;CHECK&lt;/note&gt;
              &lt;orth&gt;hlE1&lt;/orth&gt;
          &lt;/form&gt;
          &lt;form type="lemma" xml:lang="cu"&gt;
              &lt;orth&gt;lE1&lt;/orth&gt;
          &lt;/form&gt;
          &lt;form type="variant" xml:lang="cu"&gt;
              &lt;orth&gt;var5&lt;/orth&gt;
          &lt;/form&gt;
      &lt;/entry&gt;
      &lt;entry&gt;
          &lt;form type="hyperlemma" xml:lang="cu"&gt;
              &lt;orth&gt;hlE2&lt;/orth&gt;
          &lt;/form&gt;
          &lt;form type="lemma" xml:lang="cu"&gt;
              &lt;orth&gt;lE2&lt;/orth&gt;
          &lt;/form&gt;
      &lt;/entry&gt;
  &lt;/dictionary&gt;
  </code></pre>
  
  <p>A way to get a list of the distinct element names is documented in <a href="https://stackoverflow.com/questions/5861131/how-to-list-complete-xml-document-using-xslt">How to list complete XML document using XSLT</a> (please see Dimitre Novatchev's answer).</p>
  
  <p>Using this stylesheet</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0"&gt;
      &lt;xsl:output method="text"/&gt;
      &lt;xsl:strip-space elements="*" /&gt;
  
      &lt;xsl:key name="kElemByName" match="*" use="name(.)"/&gt;
  
      &lt;xsl:template match="
          *[generate-id()
          =
          generate-id(key('kElemByName', name(.))[1])
          ]"&gt;
          &lt;xsl:value-of select="concat(name(.), '&amp;#xA;')"/&gt;
          &lt;xsl:apply-templates select="*"/&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="text()"/&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>the (correct) output is</p>
  
  <pre><code>dictionary
  entry
  form
  note
  orth
  </code></pre>
  
  <p>Is it possible to get the attribute names, too? I would like to have the following output</p>
  
  <pre><code>dictionary
  entry
  form type="hyperlemma" xml:lang="cu"
  form type="lemma" xml:lang="cu"
  form type="variant" xml:lang="cu"
  note type="editor's comment"
  orth
  </code></pre>
  
  <p>How do I achieve this?</p>