0

示例代码后更新。

解决方案:正如BeniBela提供的那样,他弄清楚了我没有说清楚的......它必须是命令行,不一定是正则表达式,并提供了这个解决方案:

xpath -e '//Placemark[contains(description, "Iron")]'

按照承诺:

       |
      ( )
     /   \
    _______
   |   _   |
   |  | |  |  All must enter and pay homage! (Shrine of BeniBela)

问题:我需要某种形式的命令行正则表达式来完成以下任务:在一个文件中检测一组地标,地标在包含的 CDATA 标记中包含关键字(在本例中为 Iron)。不抓取没有关键字的地标。<Placemark>(从到的所有数据都</Placemark>需要被捕获。)

解释:

下面给出两个代码示例,一个显示三个完整的地标,其中两个对我来说没用,第三个我想要。第二个代码示例只显示了我感兴趣的那个。

我需要从数据文件(包含数百个地标)中提取有效的地标并将其附加到另一个文件中。稍后我会将此文件合并为格式正确的 KML。数据集来自美国地质调查局,非常庞大。

这里的想法是恢复正在提取给定类型矿石(本示例中为铁)的矿山的地标,并创建一个专门的 KML(Keyhole 标记语言)文件以在 Google 地球类型的应用程序中显示。

sample1(一个有效条目的多个数据):

<Placemark>
<name>
Las Antos Prospect</name>
<Snippet>
Record 10005251</Snippet>
<description>
<![CDATA[<p>
Record <a href="http://mrdata.usgs.gov/mrds/show.php?labno=10005251">
10005251</a>
 of the <a href="http://mrdata.usgs.gov/mrds/">
Mineral Resources Data System</a>
</p>
<table border='1' padding='3' cellspacing='0'>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
oper_type</th>
<td>
Unknown</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
dev_stat</th>
<td>
Occurrence</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
ore</th>
<td>
Limestone</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
model</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod1</th>
<td>
Limestone, General</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod2</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod3</th>
<td>
</td>
</tr>
</table>
]]>
</description>
<styleUrl>
#defaultStyleMap</styleUrl>
<Point>
<altitudeMode>
relativeToGround</altitudeMode>
<coordinates>
-64.88273,-24.87527,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>
Unnamed Occurence</name>
<Snippet>
Record 10005252</Snippet>
<description>
<![CDATA[<p>
Record <a href="http://mrdata.usgs.gov/mrds/show.php?labno=10005252">
10005252</a>
 of the <a href="http://mrdata.usgs.gov/mrds/">
Mineral Resources Data System</a>
</p>
<table border='1' padding='3' cellspacing='0'>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
oper_type</th>
<td>
Unknown</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
dev_stat</th>
<td>
Occurrence</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
ore</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
model</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod1</th>
<td>
Iron</td>                        ######################Iron here makes it valid
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod2</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod3</th>
<td>
</td>
</tr>
</table>
]]>
</description>
<styleUrl>
#defaultStyleMap</styleUrl>
<Point>
<altitudeMode>
relativeToGround</altitudeMode>
<coordinates>
-64.81607,-24.67527,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>
Merced I  Quarry</name>
<Snippet>
Record 10005254</Snippet>
<description>
<![CDATA[<p>
Record <a href="http://mrdata.usgs.gov/mrds/show.php?labno=10005254">
10005254</a>
 of the <a href="http://mrdata.usgs.gov/mrds/">
Mineral Resources Data System</a>
</p>
<table border='1' padding='3' cellspacing='0'>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
oper_type</th>
<td>
Unknown</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
dev_stat</th>
<td>
Producer</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
ore</th>
<td>
Limestone</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
model</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod1</th>
<td>
Limestone, General</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod2</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod3</th>
<td>
</td>
</tr>
</table>
]]>
</description>
<styleUrl>
#ProducerStyleMap</styleUrl>
<Point>
<altitudeMode>
relativeToGround</altitudeMode>
<coordinates>
-65.46052,-24.9586,0</coordinates>
</Point>
</Placemark>

上面的示例包含两个我没有用的地标,其中一个是我需要提取的。

示例 2(仅显示“有效”条目):(捕获需要抓取所有这些)

<Placemark>
<name>
Unnamed Occurence</name>
<Snippet>
Record 10005252</Snippet>
<description>
<![CDATA[<p>
Record <a href="http://mrdata.usgs.gov/mrds/show.php?labno=10005252">
10005252</a>
 of the <a href="http://mrdata.usgs.gov/mrds/">
Mineral Resources Data System</a>
</p>
<table border='1' padding='3' cellspacing='0'>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
oper_type</th>
<td>
Unknown</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
dev_stat</th>
<td>
Occurrence</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
ore</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
model</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod1</th>
<td>
Iron</td>                        ######################Iron here makes it valid
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod2</th>
<td>
</td>
</tr>
<tr valign='top'>
<th align='right' bgcolor='#ddffee'>
commod3</th>
<td>
</td>
</tr>
</table>
]]>
</description>
<styleUrl>
#defaultStyleMap</styleUrl>
<Point>
<altitudeMode>
relativeToGround</altitudeMode>
<coordinates>
-64.81607,-24.67527,0</coordinates>
</Point>
</Placemark>

更新1:

我让它在正则表达式测试器中工作,但我仍在研究如何将它放入 grep et.al。

<Placemark>\n<name>\n.*</name>\n<Snippet>\n.*\n<description>\n(?:(?:.*\n){48}.*Iron.*\n|(?:.*\n){41}.*Iron.*\n|(?:.*\n){35}.*Iron.*\n)(?:.*\n){3,16}\]\]>\n</description>\n(?:.*\n){8,12}</Placemark>
4

1 回答 1

1

这对于 XPath 而不是正则表达式来说是微不足道的:

/Placemark[contains(description, "Iron")]

(或者/*/Placemark[contains(description, "Iron")]如果您的 xml 包含(必需的)根元素)

于 2013-10-04T10:55:50.817 回答