删除 xml 标签和与 xsl 文件关联的子标签

Remove xml tag and associated child with xsl file

我遇到了有关 liquibase 更新日志的问题。我需要删除与特定子标签关联的特定标签。

关于下面的Db-Changelog,需要自动删除与子标签alterSequence

关联的标签changeSet

<?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>

感谢 Maven 插件 xml-maven-plugin,由于 xsl 样式表,可以创建另一个更改日志。但就目前而言,我做了一些尝试,但没有用。 (全部复制)


<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>

有人看到这里有什么问题吗?

在此先感谢您的支持。

如果您的处理器支持 XSLT 2.0,那么您可以:

<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>

注意 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