Replace all <time> tags in one doc with those from another

跟風遠走 提交于 2019-12-12 03:44:18

问题


I have 2 xml files which have 6000 tags in each - one also contains lots of other data, the 2nd - just 6000 tags. What I want to do is replace the tags in doc 1, with those from doc 2. So the 1st one from doc 2 replaces the 1st in doc 1, then the 2nd in doc 2 replaces the 2nd in doc 1 etc...

Doc 1 looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="Garmin Connect" version="1.1"
  xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/11.xsd"
  xmlns="http://www.topografix.com/GPX/1/1"
  xmlns:ns3="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
  xmlns:ns2="http://www.garmin.com/xmlschemas/GpxExtensions/v3"         
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <metadata>
<link href="connect.garmin.com">
  <text>Garmin Connect</text>
</link>
<time>2017-02-19T09:47:24.000Z</time>
  </metadata>
  <trk>
    <name>Hawkesbury Cycling</name>
    <type>cycling</type>
<trkseg>
  <trkpt lat="51.5749163925647735595703125" lon="-2.309663258492946624755859375">
    <ele>160.1999969482421875</ele>
    <time>1989-12-31T02:06:36.000Z</time>
    <extensions>
      <ns3:TrackPointExtension>
        <ns3:atemp>13.0</ns3:atemp>
        <ns3:hr>98</ns3:hr>
        <ns3:cad>0</ns3:cad>
      </ns3:TrackPointExtension>
    </extensions>
  </trkpt>
  <trkpt lat="51.57489853911101818084716796875" lon="-2.30963408946990966796875">
    <ele>160.600006103515625</ele>
    <time>1989-12-31T02:06:37.000Z</time>
    <extensions>
      <ns3:TrackPointExtension>
        <ns3:atemp>13.0</ns3:atemp>
        <ns3:hr>98</ns3:hr>
        <ns3:cad>29</ns3:cad>
      </ns3:TrackPointExtension>
    </extensions>
  </trkpt>
  <trkpt lat="51.5748837031424045562744140625" lon="-2.309593521058559417724609375">
    <ele>160</ele>
    <time>1989-12-31T02:06:38.000Z</time>
    <extensions>
      <ns3:TrackPointExtension>
        <ns3:atemp>13.0</ns3:atemp>
        <ns3:hr>99</ns3:hr>
        <ns3:cad>29</ns3:cad>
      </ns3:TrackPointExtension>
    </extensions>
  </trkpt>

Doc 2 looks like this:

<time>2017-02-19T09:47:24Z</time>
<time>2017-02-19T09:47:25Z</time>
<time>2017-02-19T09:47:26Z</time>

So I want an automated way to replace all the <time>Blah...</time> content from doc 2 into doc 1 - whilst not messing up the structure and other content of doc 1.

Any ideas???

So...

Line 1 in doc 2 would replace line 19 in doc 2, line 2 of doc 2 would replace line 30 of doc 2, line 3 of doc 2 would replace line 41 of doc 1. So we do have a numeric pattern here we can follow - e.g. every 11 lines after the 1st replace at line 19...

I've also now formatted doc 2 so that the line numbers match doc 1 where they need to replace - so for example the 1st tag in doc 1 is line 19, and now in doc 2 its also line 19 , 2nd tag in doc 1 is line 30, so it is in doc 2...

来源:https://stackoverflow.com/questions/42354609/replace-all-time-tags-in-one-doc-with-those-from-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!