简体   繁体   中英

Remove xml tag and associated child with xsl file

I'm facing a problem regarding a liquibase changelog. I need to delete a specific tag associated to a specific child tag.

Regarding the Db-Changelog hereunder, the need is to automatically delete the tag changeSet associated to the child tag alterSequence


<?xml version="1.1" encoding="UTF-8" standalone="no"?>

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
    <changeSet author="abc(generated)" id="1580482453362-1">
        <addColumn tableName="abc">
            <column defaultValueBoolean="true" name="abc" type="bool">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>
    <changeSet author="abc (generated)" id="1591190895113-1">
        <alterSequence sequenceName="hibernate_sequence"/>
    </changeSet>
    <changeSet author="abc (generated)" id="v1.6.0">
        <tagDatabase tag="v1.6.0" />
    </changeSet>
</databaseChangeLog>

Thanks to the Maven plugin xml-maven-plugin, it is possible to create another changelog thanks to a xsl stylesheet. But for now, I'me made some tries but it does not working. (Everything is copied)


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"            >
    <xsl:output method="xml" indent="yes" version="1.0"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>


    <xsl:template match="databaseChangeLog[changeSet[alterSequence/@sequenceName='hibernate_sequence']]" />


</xsl:stylesheet>

Does someone see what is the problem here?

In advance, thank you for your support.

If your processor supports XSLT 2.0, then you can do:

<xsl:stylesheet 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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="changeSet[alterSequence/@sequenceName='hibernate_sequence']" />

</xsl:stylesheet>

Note the xpath-default-namespace attribute on the xsl:stylesheet element. Without it, the 2nd template would match nothing, because the source XML contains a default namespace declaration xmlns="http://www.liquibase.org/xml/ns/dbchangelog" that puts all its elements in a namespace .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM