sed 异常/如果其他条件删除 xml 上的单词 [英] sed exceptions / if else condition on deleting word on xml

查看:16
本文介绍了sed 异常/如果其他条件删除 xml 上的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 sed 脚本:

I am currently using a sed script:

先cd(根文件夹)

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

目前,此脚本删除了文件夹(包括其子文件夹)上所有 pom.xml 上的 -SNAPSHOT,标记为 ,xml的例子是:

currently, this script removes the -SNAPSHOT on all pom.xml on a folder including its subfolders, under the tagging <dependencies></dependencies>, example of xml is:

 <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"或基本上是调度程序的标记,因为我不需要解析它,但是我的脚本正在删除它,因为它在依赖项标记下,如何我可以排除这个吗?调度程序"这个词会发生变化,因为我将在不同的服务上使用它,所以脚本应该依赖于这个词的排除,因为我会在使用不同的服务时改变它.

so now, what I need is to exclude those tagging which has word "scheduler-service-core" or basically scheduler, as I dont need to parse this, but my script is removing it because it is under dependencies tagging, How can I have exclusion on this one? the words "scheduler" will be changing because I will be using this on different services, so the script should be relying the exclusion on the word as I will change it when using to different services.

所需的输出应该是:

 <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>

如果您可以看到,artifactID - scheduler-service-core-SNAPSHOT 已被保留,并且其下的所有其他依赖项具有 -SNAPSHOT 将被删除.

if you can see, the -SNAPSHOT for artifactID - scheduler-service-core has been retained, and all other dependencies below which has -SNAPSHOT will be removed.

推荐答案

不要尝试使用 sed 编辑 XML,它不是为这种结构化数据而设计的.编辑 XML 的 sed 脚本总是会在有人在您原先不期望它们的地方插入良性空格时崩溃,并且编辑 XML 的人不会期望由于布局更改而中断.

Don't try to edit XML with sed, it isn't made for this kind of structured data. sed scripts that edit XML invariably break down when someone inserts benign whitespaces somewhere you didn't originally expect them, and nobody who edits XML expects things to break because of layout changes.

相反,我会使用 XSLT:

Instead, I'd use 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 包含上述样式表.

depending on which XSLT processor you like, where foobar.xsl contains the above stylesheet.

这篇关于sed 异常/如果其他条件删除 xml 上的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆