XPATH - 如何在特定的<tag>元素后选择<tag>?

时间:2015-08-11 08:51:04

标签: xpath

这是我的文档结构

for key in diff:
    if key in first:
        print 'First IP'
    elif key in second:
        print 'Second IP'

我想选择<item_name>Item 1</item_name> <item_description>Item 1 Description</item_description> <item_name>Item 2</item_name> <item_description>Item 2 Description</item_description> ... ... More <item_*> elements <item_name>Item 400</item_name> <item_description>Item 3 Description</item_description> <item_name>Item 401</item_name> <item_description>Item 4 Description</item_description> ... ... More <item_*> elements 元素作为text()

的描述

1 个答案:

答案 0 :(得分:1)

使用following-sibling。例如,假设每个item_name后跟item_description元素:

//item_name[.='Item 400' or .='Item 401']/following-sibling::item_description[1]/text()

或者如果某些item_name可能没有相应的item_description,并且您希望在这种情况下不返回任何内容:

//item_name[.='Item 400' or .='Item 401']
/following-sibling::*[1][self::item_description]
/text()