我想使用XSL
将默认名称空间定义添加到根元素。然而,问题是,在XSL
转换之后,子元素具有我需要防止的属性xmlns=''
。XSL仅将名称空间定义添加到XML根目录
XML输入:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog>
<changeSet author="abc" id="def">
...
</changeSet>
</databaseChangeLog>
预期XML输出:
<?xml version="1.0" encoding="UTF-8"?><databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
<changeSet author="abc" id="def">
...
</changeSet>
</databaseChangeLog>
我已经与XSL试过这样:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/databaseChangeLog">
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
<xsl:apply-templates select="node()|@*" />
</databaseChangeLog>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但结果是:
<?xml version="1.0" encoding="UTF-8"?><databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
<changeSet xmlns="" author="abc" id="def">
...
</changeSet>
</databaseChangeLog>
不知道如何解决这个问题?
请注意,还有一个类似的问题:XSLT xmlns on root only,但在我的情况下,XML中包含更多元素(如示例中所列的元素),所以对我来说这是不可行的。
请注意,在您的预期输出中,'xmlns =“http://www.liquibase.org/xml/ns/dbchangelog”'会为'databaseChangeLog'的所有子元素创建一个_default命名空间_。所以,它不是“仅限根”的名称空间。 –
@MathiasMüller:感谢您的纠正,随时更新问题/标题 –
在您的许可下,我为您的问题添加了一个说明。问题标题看起来不错。 –