更多级别的 XPath

XPath over more levels

是否可以使用 xpath 在另一个关卡中提取内容?

在我的 table 中有不同的不同行。在一个中,如果有我的行,某处打印 "bpmtestinstanz"。我需要获取 Link 的 URL (href),其中 "Instanz abbauen" 在 Link 和 table 行 (tr) "bpmtestinstanz"里面。但我不知道我应该如何处理 Link 和打印文本的不同级别的 xpath .

<tr class="htmlobject_tr even" onclick="tr_click(this, 'idp54bf6c72037f1')" onmouseover="tr_hover(this, 'idp54bf6c72037f1')" onmouseout="tr_hover(this, 'idp54bf6c72037f1')">

<td class="htmlobject_td state">
<span class="pill active">
active</span>
</td>

<td class="htmlobject_td config">
<b>
Request ID</b>
 14218305642887<br>
<b>
Hostname</b>
 bpmtestinstanz<br>
<b>
Typ</b>
 KVM VM (localboot)<br>
<b>
CPU</b>
 1<br>
<b>
Speicher</b>
 1.024 GB<br>
<b>
Kernel</b>
 default<br>
<b>
Festplatte</b>
 5 GB<br>
<b>
Image</b>
 13982361680940.cloud_14218305642887_1_<br>
<b>
IP</b>
 192.168.200.166, 172.26.41.105</td>

<td class="htmlobject_td comment">
Requested by user danieldrichardt<hr>
<a class="plugin console" onclick="sshwindow = window.open('https://172.26.41.105:8022','window1722641105', 'location=0,status=0,scrollbars=yes,resizable=yes,width=973,height=500,left=100,top=100,screenX=400,screenY=100'); sshwindow.focus(); return false;" href="#">
Ajaxterm</a>
<a class="plugin novnc" target="_blank" href="api.php?action=novnc&amp;appliance_id=14218305990082">
noVNC</a>
</td>

<td class="htmlobject_td action">
<a data-message="" class="pause" title="Instanz pausieren" href="index.php?cloud_ui=pause&amp;cloudappliance_id[]=14218308730556">
Instanz pausieren</a>
<a data-message="" class="restart" title="Instanz neu starten" href="index.php?cloud_ui=restart&amp;cloudappliance_id[]=14218308730556">
Instanz neu starten</a>
<a data-message="" class="private" title="Privates Image anlegen" href="index.php?cloud_ui=image_private&amp;appliance_id=14218305990082">
Privates Image anlegen</a>
<a data-message="" class="edit" title="Instanz bearbeiten" href="index.php?cloud_ui=appliance_update&amp;cloudappliance_id=14218308730556">
Instanz bearbeiten</a>
<a data-message="" class="remove" title="Instanz abbauen" href="index.php?cloud_ui=deprovision&amp;cloudappliance_id[]=14218308730556">
Instanz abbauen</a>
</td>
</tr>

可以做,就是稍微复杂了

//tr[td[@class = 'htmlobject_td config' and contains(b[contains(., 'Hostname')]
/following-sibling::text()[1],'bpmtestinstanz')]]/td[@class = 'htmlobject_td action']
/a[contains(.,'Instanz abbauen')]/@href

这意味着

//tr[td[@class = 'htmlobject_td config'             Select all `tr` elements anywhere in
                                                    the document, but only if they have
                                                    at least one `td` child element that
                                                    has a `class` attribute with the
                                                    value "htmlobject_td config"
and contains(b[contains(., 'Hostname')]             and only if this `td` child element
                                                    also has a child element `b` whose
                                                    text value contains "Hostname"
/following-sibling::text()[1],'bpmtestinstanz')]]   and if the first following sibling
                                                    of this `b` element which is a text
                                                    node contains "bpmtestinstanz"
/td[@class = 'htmlobject_td action']                of this `tr` select a child `td`
                                                    element that has a `class` attribute
                                                    with the value "htmlobject_td action"
/a[contains(.,'Instanz abbauen')]/@href             and select its child `a` if its text
                                                    contains "Instanz abbauen" - and
                                                    finally select the `href` attribute
                                                    of this `a` element.

此解决方案要求 "bpmtestinstanz" 紧跟在 <b>Hostname</b> 之后。如果它可以出现在 tr 元素中的任何位置,您可以稍微简化表达式。