Home > Enterprise >  How to remove XML Tag Blocks from the command line with one or more occurrences
How to remove XML Tag Blocks from the command line with one or more occurrences

Time:11-28

I have an xml file that looks something like this:

<xml>
  <trkseg>
  <note>
    <to>A</to>
    <from>B</from>
    <body>
      keep this
    </body>
  </trkseg>
  <trkseg>
  </note>
  ...
  </trkseg>
</xml>

And I wanted to remove all the following code. This combination of tags can occur more than once in the file:

</trkseg>
<trkseg>

Any tips on how to fix this?

What I expected was this:

<xml>
  <trkseg>
  <note>
    <to>A</to>
    <from>B</from>
    <body>
      keep this
    </body>
  </note>
  ...
  </trkseg>
</xml>

I tried using this sed command but doesn't work the way I want:

sed -i '' -e '/<\/trkseg>/,/<trkseg>/d' my-file.xml

I get this result:

<xml>
  <trkseg>
  <note>
    <to>A</to>
    <from>B</from>
    <body>
      keep this
    </body>
  </note>
  ...

CodePudding user response:

I would do this with an XSLT transformation. In XSLT 3.0 it's:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0">
  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:template match="trk">
    <xsl:copy-of select="* except *:trkseg"/>
    <xsl:copy select="*:trkseg[1]">
      <xsl:copy-of select="*:trkseg/*"/>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

CodePudding user response:

This is the actual file that I want to change with the sed command. The sed command doesn't work. It removes the closing </trkseg> in both cases. The 'fake xml file' and the gpx file.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns:tp1="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:ql="http://www.qlandkarte.org/xmlschemas/v1.1" version="1.1" xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" creator="QMapShack 1.15.2 http://www.qlandkarte.org/" xmlns="http://www.topografix.com/GPX/1/1" xmlns:rmc="urn:net:trekbuddy:1.0:nmea:rmc" xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.qlandkarte.org/xmlschemas/v1.1 http://www.qlandkarte.org/xmlschemas/v1.1/ql-extensions.xsd http://www.cluetrust.com/XML/GPXDATA/1/0 http://www.cluetrust.com/Schemas/gpxdata10.xsd">
 <metadata>
  <name>trackcombo</name>
  <time>2022-11-28T10:58:13.332Z</time>
 </metadata>
 <trk>
  <name>trackcombo</name>
  <extensions>
   <ql:key>aad886cf09191877c01ef7c4fb50eaa8</ql:key>
   <ql:flags>3</ql:flags>
   <gpxx:TrackExtension>
    <gpxx:DisplayColor>DarkBlue</gpxx:DisplayColor>
   </gpxx:TrackExtension>
  </extensions>
  <trkseg>
   <trkpt lon="-2.20870076" lat="54.29765770">
    <extensions>
     <ql:flags>16</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.20883551" lat="54.29805178">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.20864687" lat="54.29861924">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.20888941" lat="54.29893449">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.20967095" lat="54.30082597">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.20967095" lat="54.30159829">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21031773" lat="54.30255974">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21282403" lat="54.30241789">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21427930" lat="54.30162981">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21514169" lat="54.30150372">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21638136" lat="54.30194504">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21692035" lat="54.30276463">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21762104" lat="54.30330051">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21896851" lat="54.30359996">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22047768" lat="54.30364725">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22093582" lat="54.30359996">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
  </trkseg>
  <trkseg>
   <trkpt lon="-2.22091606" lat="54.30363727">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22087294" lat="54.30377123">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21985155" lat="54.30396824">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21962518" lat="54.30404704">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21980843" lat="54.30442530">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22023513" lat="54.30487710">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22056302" lat="54.30512663">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22091336" lat="54.30539193">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22156015" lat="54.30583847">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22213507" lat="54.30599081">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22280881" lat="54.30637430">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22338822" lat="54.30691538">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
  </trkseg>
  <trkseg>
   <trkpt lon="-2.22332624" lat="54.30695373">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22281060" lat="54.30745383">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22225724" lat="54.30806791">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22195541" lat="54.30859742">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22192397" lat="54.30882540">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22160327" lat="54.30903131">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22138947" lat="54.30928135">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22109392" lat="54.31018956">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22038066" lat="54.31123957">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.22025040" lat="54.31134200">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21998540" lat="54.31142867">
    <extensions>
     <ql:flags>0</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
   <trkpt lon="-2.21993150" lat="54.31153635">
    <extensions>
     <ql:flags>16</ql:flags>
     <ql:activity>0</ql:activity>
    </extensions>
   </trkpt>
  </trkseg>
 </trk>
 <extensions>
  <ql:key>b4194f3799c17e4cb8c5607b2bbf46b7</ql:key>
  <ql:sortingRoadbook>0</ql:sortingRoadbook>
  <ql:sortingFolder>0</ql:sortingFolder>
  <ql:correlation>1</ql:correlation>
  <ql:invalidDataOk>0</ql:invalidDataOk>
 </extensions>
</gpx>
  • Related