KML 根据条件在两个其他标签之间插入特定标签

KML Inserting a Specific Tag between Two Other Tags Based on a Condition

TLDR - 仅当描述中提到短语 "the office" 时,才在名称和描述标签之间插入样式标签及其内容(参见代码块),以更改当前的 Google 地球地标从默认的黄色到自定义的...

大家好,我有点难以理解这个…… 我正在使用 Notepadd++ 编辑一个 Google Earth kml 文件,其中有许多地标遵循此 XML 模式:

<Placemark>
      <name>Jim</name>
      <description>Jim goes to the office every day</description>
      <TimeSpan>
        <begin>2016-06-20T12:00:00Z</begin>
        <end>2016-06-25T12:00:00Z</end>
      </TimeSpan>
      <Point>
        <coordinates>123412341234,123412341234,1</coordinates>
      </Point>
        </Placemark>

我想找到短语“the office”的所有实例。如果找到该文本,下面的代码将以 Google earth 可读的方式插入名称和描述之间。

<Style id="customstyle">
        <IconStyle>
            <color>a1ff00ff</color>
            <scale>1.5</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png</href>
            </Icon>
        </IconStyle>
            </Style>

这样做会将地标从默认地标更改为自定义地标。

到目前为止我找到的所有教程都是关于如何使用 Notepad++ 正则表达式将单词或短语添加到行首或行尾的说明,或者说明如何使用\n. 但是,我认为我的情况是独一无二的,因为基于特定的标准我想将我的文本插入在线上方。 (更具体地说,在名称和描述标签之间插入我的文字)

最终结果看起来像这样(注意我要添加的文本现在如何位于名称标签和描述标签之间)

<Placemark>
      <name>Jim</name>
      <Style id="customstyle">
        <IconStyle>
            <color>a1ff00ff</color>
            <scale>1.5</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png</href>
            </Icon>
        </IconStyle>
        </Style>
      <description>Jim goes to the office every day</description>
      <TimeSpan>
        <begin>2016-06-15T12:00:00Z</begin>
        <end>2016-06-20T12:00:00Z</end>
      </TimeSpan>
      <Point>
        <coordinates>2135125,1234523451,12341234</coordinates>
      </Point>
    </Placemark>

现在所有遵循该模式的地标都将具有与默认地标不同类型的地标(我将不再头疼)。

在此先感谢大家。

嗯,正则表达式并不真的需要匹配行前的内容。
它只需要在你的比赛之前放一些带线的东西。
所以这仍然是一件相当简单的事情。

所以使用记事本++

查找内容:(\s+)(<description>)(?=.*?the office.*?<\/description>)

替换为:<Style id="customstyle">\t<IconStyle>\t\t<color>a1ff00ff</color>\t\t<scale>1.5</scale>\t\t<Icon>\t\t\t<href>http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png</href>\t\t</Icon>\t</IconStyle></Style>

搜索模式:正则表达式

请注意,描述标签前的空格放在捕获组 1 中。
这是一个使用与标签相同缩进的插入内容的技巧。

但您也可以只输入没有空格的标签。

查找内容:(<description>)(?=.*?the office.*?<\/description>)

替换为:<Style id="customstyle"><IconStyle><color>a1ff00ff</color><scale>1.5</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png</href></Icon></IconStyle></Style>

然后使用像 "XML Tools" 这样的插件来 "Pretty Print" 你的 XML。