Page 1 of 1

Remove KML elements that do not contain certain values

Posted: Sun May 12, 2019 5:57 am
by billswerski
Hello,

I have a large KML file that follows this format:

Code: Select all

		
<Document id="root_doc">
	<name>Perennial_Streams.kml</name>
	<Schema name="OGRGeoJSON" id="OGRGeoJSON">
		<SimpleField type="int" name="OBJECTID"></SimpleField>
		<SimpleField type="int" name="HYDROID"></SimpleField>
		<SimpleField type="string" name="FTYPE"></SimpleField>
		<SimpleField type="int" name="FCODE"></SimpleField>
		<SimpleField type="string" name="DIG_NAME"></SimpleField>
		<SimpleField type="string" name="DRAIN"></SimpleField>
		<SimpleField type="int" name="RCHID"></SimpleField>
		<SimpleField type="string" name="VISIBLE"></SimpleField>
		<SimpleField type="string" name="SOURCE"></SimpleField>
	</Schema>
	<StyleMap id="m_ylw-pushpin">
		<Pair>
			<key>normal</key>
			<styleUrl>#s_ylw-pushpin</styleUrl>
		</Pair>
		<Pair>
			<key>highlight</key>
			<styleUrl>#s_ylw-pushpin_hl</styleUrl>
		</Pair>
	</StyleMap>
	<Style id="s_ylw-pushpin_hl">
		<IconStyle>
			<scale>1.3</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
			</Icon>
			<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
		</IconStyle>
		<LineStyle>
			<color>ffffa35c</color>
			<width>3.7</width>
		</LineStyle>
		<PolyStyle>
			<color>ffffaa55</color>
		</PolyStyle>
	</Style>
	<Style id="s_ylw-pushpin">
		<IconStyle>
			<scale>1.1</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
			</Icon>
			<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
		</IconStyle>
		<LineStyle>
			<color>ffffa35c</color>
			<width>3.7</width>
		</LineStyle>
		<PolyStyle>
			<color>ffffaa55</color>
		</PolyStyle>
	</Style>
	<Folder>
		<name>OGRGeoJSON</name>
		<Placemark>
			<name>POTOMAC RIVER</name>
			<styleUrl>#m_ylw-pushpin</styleUrl>
			<ExtendedData>
				<SchemaData schemaUrl="#OGRGeoJSON">
					<SimpleData name="OBJECTID">2042</SimpleData>
					<SimpleData name="HYDROID">100006243</SimpleData>
					<SimpleData name="FTYPE">RIVER</SimpleData>
					<SimpleData name="FCODE">46010</SimpleData>
					<SimpleData name="DIG_NAME">POTOMAC RIVER</SimpleData>
					<SimpleData name="DRAIN"></SimpleData>
					<SimpleData name="RCHID">0</SimpleData>
					<SimpleData name="VISIBLE">Y</SimpleData>
					<SimpleData name="SOURCE">RPA Field</SimpleData>
				</SchemaData>
			</ExtendedData>
			<LineString>
				<extrude>1</extrude>
				<altitudeMode>relativeToGround</altitudeMode>
				<coordinates>
					-77.2485765431118,38.992016208902,30 ...  -77.24745515099529,38.9883802722356,30 
				</coordinates>
			</LineString>
		</Placemark>
		<Placemark>
		...
		</Placemark>
		<Placemark>
		...
		</Placemark>
		<Placemark>
		...
		</Placemark>
I want to be able to remove all <Placemark> elements and child elements that do not match either

<Placemark>
<name>POTOMAC RIVER</name>

or

<Placemark>
<ExtendedData>
<SchemaData schemaUrl="#OGRGeoJSON">
<SimpleData name="DIG_NAME">POTOMAC RIVER</SimpleData>

I've tried a lot of XSLT and XML Refactoring methods, and while this seems like it should be easy, I can't figure it out. Any help would be appreciated!

Re: Remove KML elements that do not contain certain values

Posted: Mon May 13, 2019 10:01 am
by adrian
Hi,

The difficulty is in writing the XPath that describes want you want to be removed. It works with Refactoring > XML Refactoring, Delete element with the following XPath specified in the Element field:

Code: Select all

Placemark[not(name='POTOMAC RIVER' or ExtendedData/SchemaData[@schemaUrl="#OGRGeoJSON"]/SimpleData[@name="DIG_NAME"]="POTOMAC RIVER")]
Regards,
Adrian