gpt4 book ai didi

xml - sed 异常/if else 条件删除 xml 上的单词

转载 作者:太空狗 更新时间:2023-10-29 12:09:32 25 4
gpt4 key购买 nike

我目前正在使用 sed 脚本:

先cd(根文件夹)

find . -name pom.xml | xargs sed -i "/<dependencies>/,/'<\/dependencies>'/s/-SNAPSHOT//"

目前,此脚本删除了 -SNAPSHOT在所有pom.xml在包含其子文件夹的文件夹中,在标记 <dependencies></dependencies> 下,xml 的示例是:

 <parent>
<groupId>com.techstack.scheduler</groupId>
<artifactId>scheduler-service</artifactId>
<version>0.0.9-SNAPSHOT</version>
</parent>

<artifactId>scheduler-webapp</artifactId>
<packaging>war</packaging>
<name>Scheduler Service Web Application</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>com.infor.techstack.scheduler</groupId>
<artifactId>scheduler-service-core</artifactId>
<version>0.0.9-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.security.authentication</groupId>
<artifactId>oauth10a-client</artifactId>
<version>0.0.26-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>xerces</artifactId>
<groupId>xerces</groupId>
</exclusion>
</exclusions>
</dependency>

所以现在,我需要排除那些带有单词“scheduler-service-core”或基本上是调度程序的标记,因为我不需要解析它,但我的脚本正在删除它,因为它在依赖项标记下,如何我可以排除这个吗? “调度程序”这个词会发生变化,因为我将在不同的服务上使用它,所以脚本应该依赖于排除这个词,因为我会在使用不同的服务时改变它。

期望的输出应该是:

 <parent>
<groupId>com.techstack.scheduler</groupId>
<artifactId>scheduler-service</artifactId>
<version>0.0.9-SNAPSHOT</version>
</parent>

<artifactId>scheduler-webapp</artifactId>
<packaging>war</packaging>
<name>Scheduler Service Web Application</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>com.infor.techstack.scheduler</groupId>
<artifactId>scheduler-service-core</artifactId>
<version>0.0.9-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.security.authentication</groupId>
<artifactId>oauth10a-client</artifactId>
<version>0.0.26</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>xerces</artifactId>
<groupId>xerces</groupId>
</exclusion>
</exclusions>
</dependency>

如果你能看到,-SNAPSHOT对于 artifactID - scheduler-service-core已保留,下面的所有其他依赖项都有 -SNAPSHOT将被删除。

最佳答案

不要尝试使用 sed 编辑 XML,它不是为这种结构化数据而设计的。编辑 XML 的 sed 脚本总是会在有人在您最初​​没有预料到的地方插入良性空格时发生故障,而编辑 XML 的人不会期望事情会因为布局更改而中断。

相反,我会使用 XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Identity template: just copy everything -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<!-- special rule for version tags that include -SNAPSHOT and whose
parent tag has an artifactId subtag that contains scheduler-service -->
<xsl:template match="//version[contains(., '-SNAPSHOT') and not(contains(../artifactId, 'scheduler-service'))]">
<xsl:copy>
<!-- copy attributes -->
<xsl:apply-templates select="@*"/>
<!-- and only use the part of the node content before -SNAPSHOT -->
<xsl:value-of select="substring-before(., '-SNAPSHOT')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

现在你可以使用例如

xsltproc foobar.xsl pom.xml

xalan -in pom.xml -xsl foobar.xsl

根据您喜欢的 XSLT 处理器,foobar.xsl 包含上述样式表。

关于xml - sed 异常/if else 条件删除 xml 上的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50615981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com