I've following xml
<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">
<changeSet author="system (generated)" id="1538720867962-1">
<createTable tableName="AS_JOURNALEVENTDETAILATTRMAP">
<column name="JOURNALEVENTTYPEID" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="JOURNALEVENTDETAILATTRID" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="LISTORDER" type="NUMBER(9, 0)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet id="c529c6ea-45c2-4ec2-8c9d-7bc935434d21" author="system">
<setTableRemarks remarks="this is wrong"
tableName="AS_JOURNALEVENTDETAILATTRMAP"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="JOURNALEVENTTYPEID"
remarks="Journal event type identifier"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="JOURNALEVENTDETAILATTRID"
remarks="Journal event detail attribute identifier"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="LISTORDER"
remarks="Order in list"/>
</changeSet>
</databaseChangeLog>
and the exact same document with name fixedremarks.xml but there is little change <setTableRemarks remarks="this is ok" tableName="AS_JOURNALEVENTDETAILATTRMAP"/>
with following template I'm trying to fix attribute remarks inside setTableRemarks but without success - I don't know how to properly copy that attribute from external xml.
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="originalChangeLog" select="document('/tmp/fixedremarks.xml')"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:key name="remarkTableName" match="setTableRemarks" use="#tableName"/>
<xsl:template match="changeSet[setTableRemarks]">
<xsl:variable name="currentRemarkTable" select="setTableRemarks/#tableName"/>
<xsl:comment select="$currentRemarkTable"/>
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:element name="setTableRemarks">
<xsl:attribute name="remarks"
select="$originalChangeLog/key('remarkTableName', $currentRemarkTable)"/>
<xsl:attribute name="tableName" select="setTableRemarks/#tableName"/>
</xsl:element>
<xsl:copy-of select="*[not(self::setTableRemarks)]"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
can somebody tell me how to map properly remark from external document?
I think you just want the identity transformation template you have plus
<xsl:template match="changeSet/setTableRemarks[key('remarkTableName', #tableName, $originalChangeLog)]/#remarks">
<xsl:attribute name="{name()}" select="key('remarkTableName', ../#tableName, $originalChangeLog)/#remarks"/>
</xsl:template>
https://xsltfiddle.liberty-development.net/6qVRKwR has an online sample (there the secondary XML is inlined as a variable for a self-contained example but if you keep your <xsl:variable name="originalChangeLog" select="document('/tmp/fixedremarks.xml')"/> it will work as well.
Related
I'm using this template:
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:uuid="http://uuid.util.java"
exclude-result-prefixes="uuid">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="changeSet[createTable]">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates select="createTable"/>
<xsl:copy-of select="*[not(self::createTable)]"/>
</xsl:copy>
<xsl:if test="createTable/#remarks">
<xsl:element name="changeSet">
<xsl:attribute name="id" select="uuid:new-uuid()"/>
<xsl:attribute name="author">system</xsl:attribute>
<xsl:element name="setTableRemarks">
<xsl:copy-of select="createTable//(#tableName,#remarks)" />
</xsl:element>
<xsl:for-each select="createTable/column[#remarks]">
<xsl:element name="setColumnRemarks">
<xsl:copy-of select="../#tableName" />
<xsl:attribute name="columnName" select="#name"/>
<xsl:copy-of select="#remarks"/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="createTable/#remarks"/>
<xsl:template match="createTable/column/#remarks"/>
</xsl:transform>
to transform xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<changeSet author="system (generated)" id="1538720867962-1">
<createTable remarks="Journal event detail attribute mapping" tableName="AS_JOURNALEVENTDETAILATTRMAP">
<column name="JOURNALEVENTTYPEID" remarks="Journal event type identifier" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="JOURNALEVENTDETAILATTRID" remarks="Journal event detail attribute identifier" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="LISTORDER" remarks="Order in list" type="NUMBER(9, 0)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
I expect that result will be:
<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">
<changeSet author="system (generated)" id="1538720867962-1">
<createTable tableName="AS_JOURNALEVENTDETAILATTRMAP">
<column name="JOURNALEVENTTYPEID" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="JOURNALEVENTDETAILATTRID" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="LISTORDER" type="NUMBER(9, 0)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet id="c85f187d-f917-4948-8c48-7b1a132dd79e" author="system">
<setTableRemarks remarks="Journal event detail attribute mapping" tableName="AS_JOURNALEVENTDETAILATTRMAP"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="JOURNALEVENTTYPEID"
remarks="Journal event type identifier"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="JOURNALEVENTDETAILATTRID"
remarks="Journal event detail attribute identifier"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="LISTORDER"
remarks="Order in list"/>
</changeSet>
</databaseChangeLog>
but instead of that I get this:
<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">
<changeSet author="system (generated)" id="1538720867962-1">
<createTable tableName="AS_JOURNALEVENTDETAILATTRMAP">
<column name="JOURNALEVENTTYPEID" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="JOURNALEVENTDETAILATTRID" type="NUMBER(9, 0)">
<constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
</column>
<column name="LISTORDER" type="NUMBER(9, 0)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet id="c85f187d-f917-4948-8c48-7b1a132dd79e" author="system">
<setTableRemarks remarks="Order in list" tableName="AS_JOURNALEVENTDETAILATTRMAP"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="JOURNALEVENTTYPEID"
remarks="Journal event type identifier"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="JOURNALEVENTDETAILATTRID"
remarks="Journal event detail attribute identifier"/>
<setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
columnName="LISTORDER"
remarks="Order in list"/>
</changeSet>
</databaseChangeLog>
note: the problem is in element setTableRemarks and attribute remarks. It's coppied from another element not from createTable as I guess it should be. Is something in my template wrong or is there another problem?
saxon library used for this is 9.8.0-14 I also tried with 9.9.0-1 but nothing has changed.
Instead of doing this...
<xsl:copy-of select="createTable//(#tableName,#remarks)" />
Do this...
<xsl:copy-of select="createTable/(#tableName,#remarks)" />
Using // is shorthand for /descendant-or-self::node()/ and so is going to search all descendant nodes of createTable, not just the node itself. As this will select multiple attributes with the same name, only the last one will be copied (as adding an attribute to a node where an existing one with the same name already exists should replace it).
this is sample xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<changeSet id="1" author="a">
<createTable tableName="TABLE1">
<column>
</column>
</createTable>
</changeSet>
<changeSet id="1-1" author="a">
<createSequence sequenceName="SEQ_TABLE1" />
</changeSet>
<changeSet id="4" author="A">
<createTable tableName="TABLE4">
<column>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
this is template:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.liquibase.org/xml/ns/dbchangelog">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="coreTables" select="('TABLE1','TABLE2')"/>
<xsl:template match="node()[not(self::*)]">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|#*"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="databaseChangeLog">
<!-- CORE-->
<xsl:comment>CORE TABLES</xsl:comment>
<xsl:variable name="coreTablesVariable" select="changeSet[createTable/#tableName=$coreTables]"/>
<xsl:comment>CORE SEQUENCES</xsl:comment>
<xsl:variable name="coreSequencesVariable" select="changeSet[createSequence[starts-with(#sequenceName, 'SEQ_') and substring-after(#sequenceName, 'SEQ_') = $coreTables]]"/>
<xsl:comment>CORE INDEXES</xsl:comment>
<xsl:variable name="coreIndexesVariable" select="changeSet[createIndex/#tableName=$coreTables]"/>
<xsl:comment>CORE FOREIGN CONSTRAINTS</xsl:comment>
<xsl:variable name="coreForeignConstraintsVariable" select="changeSet[addForeignKeyConstraint/#baseTableName=$coreTables]"/>
<xsl:comment>CORE VIEWS</xsl:comment>
<xsl:variable name="coreViewsVariable" select="changeSet[createView/#viewName=$coreTables]"/>
<xsl:call-template name="createChangeLog">
<xsl:with-param name="outputFile" select="'core-changelog.xml'"/>
<xsl:with-param name="changeLogContent" select="$coreTablesVariable,$coreSequencesVariable,$coreIndexesVariable,$coreForeignConstraintsVariable,$coreViewsVariable"/>
</xsl:call-template>
<xsl:comment>UNMATCHED</xsl:comment>
<xsl:variable name="unmatchedChangeSets"
select="changeSet[not(some $set in ($coreTablesVariable | $coreSequencesVariable | $coreIndexesVariable |$coreForeignConstraintsVariable |$coreViewsVariable) satisfies $set is .)]"/>
<xsl:call-template name="createChangeLog">
<xsl:with-param name="outputFile" select="'unmatched-changes-changelog.xml'"/>
<xsl:with-param name="changeLogContent" select="$unmatchedChangeSets"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="createChangeLog">
<xsl:param name="outputFile"/>
<xsl:param name="changeLogContent"/>
<xsl:result-document encoding="UTF-8" indent="true" method="xml" href="{$outputFile}">
<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">
<xsl:copy-of select="$changeLogContent"/>
</databaseChangeLog>
</xsl:result-document>
</xsl:template>
</xsl:transform>
I'd like to add to output xml processed inside createChangelogTemplate to each element <changeSet> another attribute (context="legacy"). I was trying to add another template which matches databaseChangelog/changeSet with additional xsl:attribute 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 CORE.
When I've added template:
<xsl:template match="changeSet" mode="legacy">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:attribute name="context">legacy</xsl:attribute>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:template>
and replaced <xsl:copy-of select="$changeLogContent"/> with <xsl:apply-templates select="$changeLogContent" mode="legacy"/> then output file core-changelog.xml was ok but file unmatched-changes-changelog.xml had no elements.
I'm using xslt 2.0 and saxon 9.8he.
When I run this I get changeSets 1 and 1-1 in core-changelog.xml, and changeSet 4 in unmatched-changelog.xml.
If you're still seeing nothing in unmatched-changelog then we need to look in more detail at exactly how you were running it.
Incidentally the selection of unmatched elements could be done as
select="changeSet except ($coreTablesVariable | $coreSequencesVariable | $coreIndexesVariable |$coreForeignConstraintsVariable |$coreViewsVariable)"/>
I'd like to apply template in which specified element contains value of array prefixed with some constant.
<xsl:variable name="coreTables"
select="('TAB1', 'TAB2')" />
<xsl:template match="node()[not(self::*)]">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|#*"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="databaseChangeLog">
<xsl:comment> CORE TABLES </xsl:comment>
<xsl:apply-templates select="changeSet[createTable/#tableName=$coreTables]"/>
<xsl:comment> CORE SEQUENCES </xsl:comment>
<xsl:apply-templates select="changeSet[createSequence/#sequenceName='SEQ_'[$coreTables]]"/>
</xsl:template>
this is sample xml:
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<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">
<changeSet id="1" author="a">
<createTable tableName="TAB1">
<column></column>
</createTable>
</changeSet>
<changeSet id="1-1" author="a">
<createSequence sequenceName="SEQ_TAB1" />
</changeSet>
<changeSet id="4" author="A">
<createTable tableName="TAB4">
<column></column>
</createTable>
</changeSet>
</databaseChangeLog>
So with with last apply-templates I'd like to match all nodes of createSequence where attribute sequenceName is SEQ_+value of some coreTables. But I don't know how to write this select or if it's even possible like this.
I'm using xslt 2 and saxon 9.8he.
There a number of ways you could do this. Here's a couple...
<xsl:apply-templates
select="changeSet[createSequence/#sequenceName = (for $i in $coreTables return concat('SEQ_', $i))]"/>
<xsl:apply-templates
select="changeSet[createSequence[starts-with(#sequenceName, 'SEQ_') and substring-after(#sequenceName, 'SEQ_') = $coreTables]]"/>
I have following xml:
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog>
<changeSet id="1" author="a">
<createTable tableName="TABLE1">
<column></column>
</createTable>
</changeSet>
<changeSet id="2" author="A">
<createTable tableName="TABLE2">
<column></column>
</createTable>
</changeSet>
<changeSet id="3" author="A">
<createTable tableName="TABLE3">
<column></column>
</createTable>
</changeSet>
<changeSet id="4" author="A">
<createTable tableName="TABLE4">
<column></column>
</createTable>
</changeSet>
</databaseChangeLog>
This is my xslt:
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="tables" select="('TABLE1','TABLE4')"/>
<xsl:template match="databaseChangeLog">
<xsl:for-each select="changeSet/createTable">
<xsl:if test="#tableName=$tables">
<xsl:value-of select="../changeSet"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:transform>
I'd like to select entire changeSet elements in which the tableName attribute will match one of $tables array value. So in this case there should be output like:
<changeSet id="1" author="a">
<createTable tableName="TABLE1">
<column></column>
</createTable>
</changeSet>
<changeSet id="4" author="A">
<createTable tableName="TABLE4">
<column></column>
</createTable>
</changeSet>
I'm using saxon 9.8he for transformations.
Instead of doing xsl:value-of which only outputs the string value of a node, you should use xsl:copy-of. And you also need to just select .. to get the parent (doing ../changeSet will try to get a sibling element named changeSet):
<xsl:template match="databaseChangeLog">
<xsl:for-each select="changeSet/createTable">
<xsl:if test="#tableName=$tables">
<xsl:copy-of select=".."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
Note you can simplify it to this...
<xsl:template match="databaseChangeLog">
<xsl:for-each select="changeSet/createTable[#tableName=$tables]">
<xsl:copy-of select=".."/>
</xsl:for-each>
</xsl:template>
Or even just this...
<xsl:template match="databaseChangeLog">
<xsl:copy-of select="changeSet[createTable/#tableName=$tables]" />
</xsl:template>
this is sample xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<changeSet id="1" author="a">
<createTable tableName="TABLE1">
<column>
</column>
</createTable>
</changeSet>
<changeSet id="1-1" author="a">
<createSequence sequenceName="SEQ_TABLE1" />
</changeSet>
<changeSet id="4" author="A">
<createTable tableName="TABLE4">
<column>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
this is template:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.liquibase.org/xml/ns/dbchangelog">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="coreTables" select="('TABLE1','TABLE2')"/>
<xsl:template match="node()[not(self::*)]">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|#*"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="databaseChangeLog">
<!-- CORE-->
<xsl:comment>CORE TABLES</xsl:comment>
<xsl:variable name="coreTablesVariable" select="changeSet[createTable/#tableName=$coreTables]"/>
<xsl:comment>CORE SEQUENCES</xsl:comment>
<xsl:variable name="coreSequencesVariable" select="changeSet[createSequence[starts-with(#sequenceName, 'SEQ_') and substring-after(#sequenceName, 'SEQ_') = $coreTables]]"/>
<xsl:comment>CORE INDEXES</xsl:comment>
<xsl:variable name="coreIndexesVariable" select="changeSet[createIndex/#tableName=$coreTables]"/>
<xsl:comment>CORE FOREIGN CONSTRAINTS</xsl:comment>
<xsl:variable name="coreForeignConstraintsVariable" select="changeSet[addForeignKeyConstraint/#baseTableName=$coreTables]"/>
<xsl:comment>CORE VIEWS</xsl:comment>
<xsl:variable name="coreViewsVariable" select="changeSet[createView/#viewName=$coreTables]"/>
<xsl:call-template name="createChangeLog">
<xsl:with-param name="outputFile" select="'core-changelog.xml'"/>
<xsl:with-param name="changeLogContent" select="$coreTablesVariable,$coreSequencesVariable,$coreIndexesVariable,$coreForeignConstraintsVariable,$coreViewsVariable"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="createChangeLog">
<xsl:param name="outputFile"/>
<xsl:param name="changeLogContent"/>
<xsl:result-document encoding="UTF-8" indent="true" method="xml" href="{$outputFile}">
<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">
<xsl:copy-of select="$changeLogContent"/>
</databaseChangeLog>
</xsl:result-document>
</xsl:template>
</xsl:transform>
I'd like to add to output xml processed inside createChangelogTemplate to each element <changeSet> another attribute (context="legacy"). I was trying to add another template which matches databaseChangelog/changeSet with additional xsl:attribute 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 CORE.
I'm using xslt 2.0 and saxon 9.8he.
Use a separate mode i.e. instead of <xsl:copy-of select="$changeLogContent"/> use <xsl:apply-templates select="$changeLogContent" mode="legacy"/>, then set up e.g.
<xsl:template match="changeSet" mode="legacy">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:attribute name="context">legacy</xsl:attribute>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:template>
If further processing of the attributes and or the child nodes is necessary then change the <xsl:copy-of select="#*"/> and/or the <xsl:copy-of select="node()"/> to use xsl:apply-templates mode="#current" and set up further templates for the mode that perform any processing.