【问题标题】:Add XML node based on two of parents child values in the shell基于 shell 中的两个父子值添加 XML 节点
【发布时间】:2020-12-22 02:59:42
【问题描述】:

我该如何更改:

...
<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 节点的父节点。

【问题讨论】:

  • 你从哪里得到 project.basedir 变量?
  • maven - 但这不是问题(更新它)
  • 当你说依赖“dependencies/dependency/groupId”和“/dependencies/dependency/artifactId”时,逻辑是什么?它是否使用这些字段中的值?是否仅在存在某些值时才更新?
  • 添加了进一步的解释

标签: xml sh xmlstarlet


【解决方案1】:

尝试类似:

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 

【讨论】:

  • @eventhorizo​​n 缺少什么?
  • 看我的回答。你的建议完全正确。缺少的重要的东西 - 杂耍但很重要 - 是命名空间“东西”......其他人可能有同样的问题,但对它了解不够......
【解决方案2】:

在@Jack Fleeting 和来自@uk4sx (XMLStarlet does not select anything) 的帖子的帮助下 - 必须设置命名空间 - 我终于让它像这样运行(它从 Dockerfile 中运行):

RUN xmlstarlet ed -L -N my=http://maven.apache.org/POM/4.0.0 \
    --subnode "//my:dependencies/my:dependency[my:artifactId[./text()='abc']][my:groupId[./text()='xyz']]" \
      --type elem -n scope      -v "system" \
    --subnode "//my:dependencies/my:dependency[my:artifactId[./text()='abc']][my:groupId[./text()='xyz']]" \
      --type elem -n systemPath -v "\.../src/main/.../some.jar" \
    ./pom.xml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2020-08-25
    相关资源
    最近更新 更多