<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <!--
    =========================================================================
    Parameters:  defaults          The file containing the default target
                                   definitions.  This value is the default
                                   when not passed into the stylesheet.
    =========================================================================
  -->
  <xsl:param name="defaults"/>

  <!--
    =========================================================================
    Variables:  local_targets      The list of all target names found in the
                                   source tree

                default_targets    A subtree from the default process template
                                   containing the default targets.

                added_targets      A subtree containing only the default
                                   targets not present in the source tree.
    =========================================================================
  -->
  <xsl:variable name="local_targets"
                select="/project/target/@name"/>

  <xsl:variable name="default_targets"
                select="document($defaults)/defaults/default"/>

  <xsl:variable name="added_targets"
                select="$default_targets[not(@name=$local_targets)]"/>

  <!--
    =========================================================================
      Purpose:  Provide special processing when the <project> element is
                parsed.

                This is where the default <target> elements are added.  Note,
                if a target with the same name is already present in the
                source tree, it is not over-written.
    =========================================================================
  -->
  <xsl:template match="/project">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>

      <xsl:text disable-output-escaping="yes">
  &lt;!--
    =========================================================================
    =========================================================================
      Begin default targets:
    =========================================================================
    =========================================================================
  --&gt;
      </xsl:text>
      <xsl:for-each select="$added_targets">
        <xsl:apply-templates/>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

  <!--
    =========================================================================
      Purpose:  Copy each node and attribute, exactly as found, to the output
                tree.
    =========================================================================
  -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!--
    =========================================================================
      Purpose:  Supress the <default> element from being written to the
                output tree.
    =========================================================================
  -->
  <xsl:template match="default">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:template>
</xsl:stylesheet>
