2015-01-21 68 views
1

xpath是否可以在另一个Level中提取某些内容?XPath over more levels

在我的表中有不同的不同行。在一个,如果它有我的行在哪里打印“bpmtestinstanz”。我需要在链接中获取链接的URL(href),以及链接中的“Instanz abbauen”以及表格行(tr)“bpmtestinstanz”中的URL(href)。但我不知道如何处理链接和打印文本的不同级别的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> 

回答

0

它可以完成,但它有点复杂。

//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元素的任何位置,则可以稍微简化表达式。

+0

其每次后主机名。帮助我很多。谢谢:) – user3392820 2015-01-21 12:54:51

+0

@ user3392820不客气。如果你喜欢,也可以看看解释。 – 2015-01-21 13:12:01