Q:

XSLT 1.0, XML to CSV: how to retrieve headers from attributes and child nodes

Considering the following XML input:
<LIST>
    <DIRECTORIES>
        <DIRECTORY ID="000001">
            <NAME>folder</NAME>
            <LOCATION>C:\here</LOCATION>
        </DIRECTORY>
        <DIRECTORY ID="000002">
            <COLOR>blue</COLOR>
            <LOCATION>C:\here</LOCATION>
            <DATE>01-25-2015</DATE>
        </DIRECTORY>
    <DIRECTORIES>
    <FILES>
        <FILE ID="000001">
            <NAME>file</NAME>
            <TYPE>rt</TYPE>
            <SERVICE>inf</SERVICE>
        </FILE>
        <FILE ID="000002">
            <NAME>f</NAME>
        </FILE>
    </FILES>
<LIST>

I'd like to retrieve the content of DIRECTORIES, with one header per distinct DIRECTORY's child node and attributes.  In this example, the desired output would be:
ID, NAME, LOCATION, COLOR, DATE
000001, folder, C:\here,,
000002,,C:\here,blue,01-25-2015

There are a lot of questions regarding XML to CSV transformations, so I tried to tweak a solution offered in another one, without success.  Here's my XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:variable name="delimiter" select="','"/>

  <xsl:key name="directoryFields" match="/LIST/DIRECTORIES/DIRECTORY/*" use="local-name()"/>

  <xsl:variable name="dirFields"
       select="/LIST/DIRECTORIES/DIRECTORY/*[generate-id()=generate-id(key('directoryFields', local-name())[1])]" />

  <xsl:template match="/">
    <xsl:for-each select="$dirFields">
      <xsl:value-of select="local-name()" />
      <xsl:if test="position() &lt; last()">
        <xsl:value-of select="$delimiter" />
      </xsl:if>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates select="*/*" />
  </xsl:template>

  <xsl:template match="*">
    <xsl:variable name="this" select="." / >
    <xsl:for-each select="$dirFields">
      <xsl:value-of select="$this/*[local-name() = local-name(current())]" />
      <xsl:if test="position() &lt; last()">
        <xsl:value-of select="$delimiter" />
      </xsl:if>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

A:

Well:
<xsl:apply-templates select="*/*" />

needs to be: 
<xsl:apply-templates select="LIST/DIRECTORIES/DIRECTORY" />

What you have selects LIST/DIRECTORIES.  And you cannot use */*/* because that selects the FILE nodes, too. 
You also need to remove this line:
<xsl:value-of select="/LIST/DIRECTORIES/DIRECTORY"/>

added
In order to include the ID, you must add this:
<xsl:value-of select="concat(@ID, $delimiter)" />

to the template processing DIRECTORY, before moving on to process the fields.