2011-11-02 64 views
-4
之間選擇元素

可能重複:
xpath select elements between two nodes如何兩個標籤

基於這個網站我需要兩個XPath表達式:

<table dir = "rtl ......."> 
<tbody> 
<script src = "get.aspx?type=js&file=ajax&rev=3"......> 
<script language = "JavaScript" src = "get.aspx?type=js&file=mc&rev=6"></script> 
<script>..</script> 
<tr> 
<td class = "d2"...>..</td> 
</tr> 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
<tr>..</tr> <-- first expression should select these elements 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
<tr> 
<td class = "d2"...>..</td> 
</tr> 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
<tr>..</tr> <-- second expression should select these elements 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
<tr>..</tr> <-- 
</tbody> 

我如何可以選擇所有<tr>元素之後的第一個<td class = "d2"...>標記帶有xpath表達式和全部<tr>元素之後的第二個<td class = "d2"...>標記與其他xpath表達式。

+0

@Oded我只想返回這些元素的數量做一些事情。 – NouNou

+0

你還沒有回答我的問題。你嘗試了什麼?你嘗試了什麼xpath? – Oded

+0

我正在邁克iphone應用程序,並創建一個tableView與部分和我需要設置節的行數到一個特定的值,因爲我需要計算這些元素。我使用HPPLE解析HTML。 – NouNou

回答

0

您可以採取兩種節點序列的交集XPath 2.0中,像這樣

//table/tbody/tr[td/@class = 'd2'][1]/following-sibling::tr except //table/tbody/tr[td/@class = 'd2'][2]/following-sibling::tr 

在那表情,你會得到遵循包含類D2的第一個TD的TR中的所有TR兄弟姐妹,和然後刪除第二個這樣的tr之後的那些。

不幸的是,XPath 1.0沒有兩個節點集相交的概念。但是,你可以得到兩個節點集工會XPath 1.0中,像這樣:

//table/tbody/tr[td/@class = 'd2'][1]/following-sibling::tr | //table/tbody/tr[td/@class = 'd2'][2]/preceding-sibling::tr 

下面的路徑將讓你包含D2級的TD第二TR後的TR節點

//table/tbody/tr[td/@class = 'd2'][2]/following-sibling::tr 
+0

我正在使用XPath 1.0 – NouNou

+0

我更新了答案,以顯示使用XPath 1.0 – Mjonir74

+0

@ Mjonir74感謝它的工作時的替代路徑。 – NouNou