title: XSLT add attributes to processed nodes
body: <p>this is sample xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"                       
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                       
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog                      
          http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd                       
          http://www.liquibase.org/xml/ns/dbchangelog"&gt;
    &lt;changeSet id="1" author="a"&gt;
      &lt;createTable tableName="TABLE1"&gt;
        &lt;column&gt;
        &lt;/column&gt;
      &lt;/createTable&gt;
    &lt;/changeSet&gt;
    &lt;changeSet id="1-1" author="a"&gt;
      &lt;createSequence sequenceName="SEQ_TABLE1" /&gt;
    &lt;/changeSet&gt;
    &lt;changeSet id="4" author="A"&gt;
      &lt;createTable tableName="TABLE4"&gt;
        &lt;column&gt;
        &lt;/column&gt;
      &lt;/createTable&gt;
    &lt;/changeSet&gt;
  &lt;/databaseChangeLog&gt;
  </code></pre>
  
  <p>this is template:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"               xpath-default-namespace="http://www.liquibase.org/xml/ns/dbchangelog"&gt;
    &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt;
    &lt;xsl:variable name="coreTables"                  select="('TABLE1','TABLE2')"/&gt;
    &lt;xsl:template match="node()[not(self::*)]"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates/&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="*"&gt;
      &lt;xsl:element name="{local-name()}"&gt;
        &lt;xsl:apply-templates select="node()|@*"/&gt;
      &lt;/xsl:element&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="@*"&gt;
      &lt;xsl:attribute name="{local-name()}"&gt;
        &lt;xsl:value-of select="."/&gt;
      &lt;/xsl:attribute&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="databaseChangeLog"&gt;
      &lt;!-- CORE--&gt;
      &lt;xsl:comment&gt;CORE TABLES&lt;/xsl:comment&gt;
      &lt;xsl:variable name="coreTablesVariable" select="changeSet[createTable/@tableName=$coreTables]"/&gt;
      &lt;xsl:comment&gt;CORE SEQUENCES&lt;/xsl:comment&gt;
      &lt;xsl:variable name="coreSequencesVariable" select="changeSet[createSequence[starts-with(@sequenceName, 'SEQ_') and substring-after(@sequenceName, 'SEQ_') = $coreTables]]"/&gt;
      &lt;xsl:comment&gt;CORE INDEXES&lt;/xsl:comment&gt;
      &lt;xsl:variable name="coreIndexesVariable" select="changeSet[createIndex/@tableName=$coreTables]"/&gt;
      &lt;xsl:comment&gt;CORE FOREIGN CONSTRAINTS&lt;/xsl:comment&gt;
      &lt;xsl:variable name="coreForeignConstraintsVariable" select="changeSet[addForeignKeyConstraint/@baseTableName=$coreTables]"/&gt;
      &lt;xsl:comment&gt;CORE VIEWS&lt;/xsl:comment&gt;
      &lt;xsl:variable name="coreViewsVariable" select="changeSet[createView/@viewName=$coreTables]"/&gt;
      &lt;xsl:call-template name="createChangeLog"&gt;
        &lt;xsl:with-param name="outputFile" select="'core-changelog.xml'"/&gt;
        &lt;xsl:with-param name="changeLogContent" select="$coreTablesVariable,$coreSequencesVariable,$coreIndexesVariable,$coreForeignConstraintsVariable,$coreViewsVariable"/&gt;
      &lt;/xsl:call-template&gt;
  
      &lt;xsl:comment&gt;UNMATCHED&lt;/xsl:comment&gt;
      &lt;xsl:variable name="unmatchedChangeSets" 
          select="changeSet[not(some $set in ($coreTablesVariable | $coreSequencesVariable | $coreIndexesVariable |$coreForeignConstraintsVariable |$coreViewsVariable) satisfies $set is .)]"/&gt;
      &lt;xsl:call-template name="createChangeLog"&gt;
        &lt;xsl:with-param name="outputFile" select="'unmatched-changes-changelog.xml'"/&gt;
        &lt;xsl:with-param name="changeLogContent" select="$unmatchedChangeSets"/&gt;
      &lt;/xsl:call-template&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template name="createChangeLog"&gt;
      &lt;xsl:param name="outputFile"/&gt;
      &lt;xsl:param name="changeLogContent"/&gt;
      &lt;xsl:result-document encoding="UTF-8" indent="true" method="xml" href="{$outputFile}"&gt;
        &lt;databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"                                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                               xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog                                               http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd                                               http://www.liquibase.org/xml/ns/dbchangelog" logicalFilePath="TODO"&gt;
          &lt;xsl:copy-of select="$changeLogContent"/&gt;
        &lt;/databaseChangeLog&gt;
      &lt;/xsl:result-document&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:transform&gt;
  </code></pre>
  
  <p>I'd like to add to output xml processed inside <code>createChangelogTemplate</code> to each element <code>&lt;changeSet&gt;</code> another attribute (<code>context="legacy"</code>). I was trying to add another template which matches <code>databaseChangelog/changeSet</code> with additional <code>xsl:attribute</code> element but that didn't worked for me. If is there way how to do this in one place that would be nice, because I will need to prepare more sections like <code>CORE</code>.</p>
  
  <p>When I've added template:</p>
  
  <pre><code>&lt;xsl:template match="changeSet" mode="legacy"&gt;
    &lt;xsl:copy&gt;
      &lt;xsl:copy-of select="@*"/&gt;
      &lt;xsl:attribute name="context"&gt;legacy&lt;/xsl:attribute&gt;
      &lt;xsl:copy-of select="node()"/&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>and replaced <code>&lt;xsl:copy-of select="$changeLogContent"/&gt;</code> with <code>&lt;xsl:apply-templates select="$changeLogContent" mode="legacy"/&gt;</code> then output file <code>core-changelog.xml</code> was ok but file <code>unmatched-changes-changelog.xml</code> had no elements.</p>
  
  <p>I'm using xslt 2.0 and saxon 9.8he.</p>