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

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

问题描述

我当前正在使用sed脚本:

I am currently using a sed script:

cd(根文件夹)

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

当前,此脚本在标记<dependencies></dependencies>下的文件夹(包括其子文件夹)中的所有pom.xml上,删除所有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,它不是针对这种结构化数据而设计的. sed编辑XML的脚本总是会在有人在您原本不希望的地方插入良性空格时崩溃,而没有编辑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天全站免登陆