如何在 XPath 中使用 AND select 具有特定属性的特定元素?
How to select specific elements with certain attributes using AND in XPath?
如果元素名称是 [x 或 y 或 z] 并且具有已定义的属性,我想 select 同一棵树中的所有元素。在 PHP 中工作的例子是:
xpath = "//*[name()='x' or name()='y' or name()='z'][descendant::w[@attrib][@attrib2='".$variable."']]";
所以我试图在 python 2.7 中编写它的等价物:
xpath = "[//*[//x or //y or //z][descendant::w[@attrib][@attrib2='".{0}."']]".format(variable)
但这对我不起作用。其实我不知道为什么 php 代码使用 name() ?我可以而且应该在 python 中做同样的事情吗?
而不是 name()='foo'
我宁愿使用 self::foo
。以下 XPath 应该等同于您的第一个 XPath:
xpath = '''//*[self::x or self::y or self::z]
[descendant::w[@attrib and @attrib2='{0}']]
'''.format(variable)
如果元素名称是 [x 或 y 或 z] 并且具有已定义的属性,我想 select 同一棵树中的所有元素。在 PHP 中工作的例子是:
xpath = "//*[name()='x' or name()='y' or name()='z'][descendant::w[@attrib][@attrib2='".$variable."']]";
所以我试图在 python 2.7 中编写它的等价物:
xpath = "[//*[//x or //y or //z][descendant::w[@attrib][@attrib2='".{0}."']]".format(variable)
但这对我不起作用。其实我不知道为什么 php 代码使用 name() ?我可以而且应该在 python 中做同样的事情吗?
而不是 name()='foo'
我宁愿使用 self::foo
。以下 XPath 应该等同于您的第一个 XPath:
xpath = '''//*[self::x or self::y or self::z]
[descendant::w[@attrib and @attrib2='{0}']]
'''.format(variable)