我有changelog_master.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.4.xsd">
<include file="changelog/version/changelog_1.0.xml"/>
</databaseChangeLog>
我想在我的文件的close标签上面插入文字,就像这样
<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.4.xsd">
<include file="changelog/version/changelog_1.0.xml"/>
<include file="changelog/version/changelog_1.1.xml"/>
</databaseChangeLog>
我尝试使用这样的sed,因为我需要在Jenkinsfile中编写它以实现自动化
sh "sed -i '/<\/databaseChangelog>/i <include file=\"changelog/version/changelog_${version}.xml\"/>' changelog/changelog_master.xml"
但它不起作用。我也尝试了很多相关的问题。
我现在应该怎么做?
谢谢!
添加子节点并在带有名称空间的XML文件中插入带有xmlstarlet
的属性:
xmlstarlet edit \
--omit-decl -N x="http://www.liquibase.org/xml/ns/dbchangelog" \
--subnode "//x:databaseChangeLog" -t elem -n "include" \
--insert "//x:databaseChangeLog/include" -t attr -n "file" --value "changelog/version/changelog_1.1.xml" file.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.4.xsd">
<include file="changelog/version/changelog_1.0.xml"/>
<include file="changelog/version/changelog_1.1.xml"/>
</databaseChangeLog>
如果要在其中编辑文件,请添加选项-L。
见:xmlstarlet edit --help
sed也有效,这是我的版本(你的匹配模板中有拼写错误)
$ sed -i "/<\/databaseChangeLog>/i\<include file=\"changelog/version/changelog_${version}.xml\"/>" changelog/changelog_master.xml