XPath:如果当前节点有特殊标记,则为路径



我有以下xml,我在python 中使用lxml

<foo>
<bar>
<message>pick/me</message>
</bar>
<bar>
<message>not/me/though</message>
</bar>
<foo>

我重复";条";容器,并且只想基于直接子对象选择其中的某个子集。我尝试了下面的代码。

我的python代码是

lst_my_bar = root.xpath("bar")
for my_bar in lst_my_bar:
nodes_to_select_try1 = my_bar.xpath('[message/text()="pick/me"]') # does not work
nodes_to_select_try2 = my_bar.xpath('./[message/text()="pick/me"]') # does not work
nodes_to_select_try3 = my_bar.xpath('.[message/text()="pick/me"]') # does not work

如果您在bar轴上,请尝试使用以下XPath:

message[text()="pick/me"]

导致python命令

nodes_to_select_try1 = my_bar.xpath('message[text()="pick/me"]')

其应当选择相应的CCD_ 2元素
也许您必须将初始命令调整为

lst_my_bar = root.xpath("/foo/bar")

以到达CCD_ 3轴。

最新更新