【问题标题】:xpath get many nodes at oncexpath 一次获取多个节点
【发布时间】:2018-11-21 07:35:45
【问题描述】:

我是 Xpath 的新手,现在几个小时都无法解决这个问题。 我收到了来自maps.googleapis.com/maps/api/geocode/xml?latlng=51,0 的 xml 响应 其中包含多个“结果”,每个“结果”可能包含多个“地址组件”。 响应是在特定的 prog 中解析的,它不能使用像

这样的连接
/GeocodeResponse[1]/status[text()='OK'] | /GeocodeResponse[1]/result[1]/type | /GeocodeResponse[1]/result[1]/address_component[1]...

它只能通过一个请求来完成,比如

/GeocodeResponse[1]/[self::status[text()='OK'] or self::result[1]/type or self::result[1]/address_component[1]] 

但是这个路径返回所有“结果”,而我只需要第一个和其中的节点(“type”,“formatted_address”)和某个“address_component”的子节点(“long_name”,“short_name”) "的节点。 输出示例:

GeocodeResponse[1]/status
GeocodeResponse[1]/result[1]/type[1]
GeocodeResponse[1]/result[1]/formatted_address[1]
GeocodeResponse[1]/result[1]/address_component/short_name - where "address_component" contains "types" with text ('street_number', 'route', 'locality', 'administrative_area_level_1', 'postal_code')

源 xml 响应如下所示:

`<GeocodeResponse>
<status>OK</status>
<result>
<type>establishment</type>
<type>point_of_interest</type>
<formatted_address>2 Hazel Grove, Radcliffe, Uckfield M26 1DD, UK</formatted_address>
<address_component>
<long_name>2</long_name>
<short_name>2</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Hazel Grove</long_name>
<short_name>Hazel Grove</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Radcliffe</long_name>
<short_name>Radcliffe</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Uckfield</long_name>
<short_name>Uckfield</short_name>
<type>postal_town</type>
</address_component>
<address_component>
<long_name>East Sussex</long_name>
<short_name>East Sussex</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>M26 1DD</long_name>
<short_name>M26 1DD</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>51.0000000</lat>
<lng>0.0000000</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>50.9986510</lat>
<lng>-0.0013490</lng>
</southwest>
<northeast>
<lat>51.0013490</lat>
<lng>0.0013490</lng>
</northeast>
</viewport>
</geometry>
<place_id>ChIJ_8dXPfiLdUgR9wQihY2gsgk</place_id>
<plus_code>
<global_code>9F322222+22</global_code>
<compound_code>2222+22 Sheffield Green, United Kingdom</compound_code>
</plus_code>
</result>
<result>
<type>postal_code</type>
<formatted_address>Uckfield TN22 3QS, UK</formatted_address>
<address_component>
<long_name>TN22 3QS</long_name>
<short_name>TN22 3QS</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>Uckfield</long_name>
<short_name>Uckfield</short_name>
<type>postal_town</type>
</address_component>
<address_component>
<long_name>East Sussex</long_name>
<short_name>East Sussex</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>50.9973315</lat>
<lng>0.0079172</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>50.9959291</lat>
<lng>-0.0003802</lng>
</southwest>
<northeast>
<lat>51.0023266</lat>
<lng>0.0087596</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>50.9959291</lat>
<lng>-0.0003802</lng>
</southwest>
<northeast>
<lat>51.0023266</lat>
<lng>0.0087596</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJqWgEbhpg30cRL6VL5HVT3NI</place_id>
</result>
<result>...</result>
<result>...</result>
<result>...</result>
<result>...</result>
<result>...</result>
<result>...</result>
<result>...</result>
<plus_code>
<global_code>9F322222+22</global_code>
<compound_code>2222+22 Sheffield Green, UK</compound_code>
</plus_code>
</GeocodeResponse>

谁能建议我如何做到这一点?

e.g.: vals to grab

【问题讨论】:

  • 你能明确分享当前和预期的结果吗?
  • 请显示您的预期输出。您的 Xpath 和描述并不完全清楚。
  • 我需要装扮:GeocodeResponse[1]/status; GeocodeResponse[1]/result[1]/type[1]; GeocodeResponse[1]/result[1]/formatted_address[1]; GeocodeResponse[1]/result[1]/address_component/short_name - 其中 address_component 包含 ('street_number', 'route', 'locality', 'administrative_area_level_1', 'postal_code') 中的类型。
  • 失败示例是 xpath /GeocodeResponse[1]/[self::status[text()='OK']/text() 或 self::result/address_component/long_name[text()= 'Hazel Grove']] 返回“状态”并且只返回第一个“结果”。但我想明确地 result[1] 而不是整个“result”节点,而只匹配“address_component/long_name”

标签: google-maps xpath xml-parsing


【解决方案1】:

根据我对您的问题(包括您的 cmets)的理解,这是您想要的吗?

//GeocodeResponse[status='OK']/result[address_component/long_name[.='Hazel Grove']][1]

它获取GeocodeResponse 节点的第一个result 子节点(其子状态= OK)。 result 节点有一个后代 long_name(其值为 'Hazel Grove')。

编辑

请改用这个 xpath:

//GeocodeResponse[status='OK']/result[address_component/long_name[.='Hazel Grove']][1]/(preceding-sibling::status|type[1]|formatted_address[1]|address_component/short_name[../type='street_number' or ../type='route' or ../type='locality' or ../type='administrative_area_level_1' or ../type='postal_code'])

根据我对您的 XML 的测试,它会输出

OK
establishment
2 Hazel Grove, Radcliffe, Uckfield M26 1DD, UK
2
Hazel Grove
Radcliffe
England
M26 1DD

【讨论】:

  • 我不需要一个完整的“结果”节点,而只需要其中的一些子节点和孙子节点。结果必须是一个节点集。将图片添加到主题中,并突出显示所需的值
  • whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm 测试了您的示例(不管它有多长),它再次完全只抓取“结果”节点
  • 我再次测试了它,它显示了相同的结果。访问此链接 (goo.gl/2NkHqn)
猜你喜欢
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 2015-03-30
  • 2012-09-23
  • 2021-10-23
相关资源
最近更新 更多