问题描述
我有一个 pom.xml,我想更改以特定模式开始的标签的属性值.
I have a pom.xml,that I want to change the properties values for tags that starting in a certain pattern.
我通常使用 xmlstarlet 来操作 XML,但我从来没有用regex"做过,这可能吗?
I usually use xmlstarlet to manipulate XML but I never did it with "regex",Is that possible?
我的pom.xml是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.vector</groupId>
<artifactId>company-vector</artifactId>
<version>1.16-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<modules>
<module>company-vector-adapters-solaredge</module>
<module>company-vector-topology-datasource-icp</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
</pluginManagement>
</build>
<properties>
<repository.snapshots.uri>repository/maven-snapshots</repository.snapshots.uri>
<company.mgrid.common.version>0.8-SNAPSHOT</company.mgrid.common.version>
<company.mgrid.infra.version>0.8-SNAPSHOT</company.mgrid.infra.version>
<company.mgrid.globals.version>0.8-SNAPSHOT</company.mgrid.globals.version>
<java.jdk.version>1.8</java.jdk.version>
</properties>
</project>
并且我需要将 节点下以 company.mgrid* 开头的所有标签中的值从 0.8-SNAPSHOT 替换为 0.9-SNAPSHOT,
And I need to replace values in all tags under node that starts with company.mgrid* from 0.8-SNAPSHOT to 0.9-SNAPSHOT,
所以输出将是:
<properties>
<repository.snapshots.uri>repository/maven-snapshots</repository.snapshots.uri>
<company.mgrid.common.version>0.9-SNAPSHOT</company.mgrid.common.version>
<company.mgrid.infra.version>0.9-SNAPSHOT</company.mgrid.infra.version>
<company.mgrid.globals.version>0.9-SNAPSHOT</company.mgrid.globals.version>
<java.jdk.version>1.8</java.jdk.version>
</properties>
如前所述,我更喜欢用 xmlstarlet 来做这件事,但如果你有一个使用 sed 或任何其他 bash 工具的解决方案,我们将不胜感激.帮助任何人?
As mentioned I prefer to do it with xmlstarlet but if you have a solution with sed or any other bash tool it will be appreciated.Help anyone?
推荐答案
您感兴趣的是 Xpath 函数 starts-with
开始.它是自 Xpath 1.0 以来的标准功能.这允许您执行以下操作:
What you are interested in is the Xpath function starts-with
. It is a standard function since Xpath 1.0. This allows you to do :
$ xmlstarlet ed -N N="http://maven.apache.org/POM/4.0.0" \
-u "//N:*[starts-with(name(),'company.mgrid')]" \
-v "0.9-SNAPSHOT" pom.xml
这篇关于xmlstarlet 在模式后替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!