Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

linux - sed exceptions / if else condition on deleting word on xml

I am currently using a sed script:

cd (root folder) first

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

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>

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.

desired output should be:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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>

Now you can use e.g.

xsltproc foobar.xsl pom.xml

or

xalan -in pom.xml -xsl foobar.xsl

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...