首页 > 解决方案 > 基于 shell 中的两个父子值添加 XML 节点

问题描述

我该如何改变这个:

...
<dependencies>
    ...
    <dependency>
        <groupId>xyz</groupId>
        <artifactId>abc</artifactId>
    </dependency>
    ...
</dependencies>
...

对此:

...
<dependencies>
    ...
    <dependency>
        <groupId>xyz</groupId>
        <artifactId>abc</artifactId>
        <scope>system</scope>
        <systemPath>/.../src/main/.../some.jar</systemPath>
    </dependency>
    ...
</dependencies>
...

基于“/dependencies/dependency/groupId”和“/dependencies/dependency/artifactId”的值

在 bash 中(通过 xmlstarlet,...)?

更新

显然,这似乎还不够清楚。所以:代码必须找到 groupId==xyz 和 artifactId==abc 的依赖关系,然后(并且只有那时)将两个 now 节点添加到 groupId 和 artifactId 节点的父节点。

标签: xmlshxmlstarlet

解决方案


尝试类似:

xmlstarlet ed \
--subnode "//dependency[//artifactId[./text()='abc']][//groupId[./text()='xyz']]" --type elem -n scope -v "system" \
--subnode "//dependency[//artifactId[./text()='abc']][//groupId[./text()='xyz']]" --type elem -n systemPath -v "some.jar" \
file.xml 

推荐阅读