2017-08-09 70 views
0

我想把每個單元格的所有信息放在一行中的表格中。我需要弄清楚如何打印表格中每列的標題。如何使用XPath獲取表格中的上述單元格?

td, table { 
 
    border: 2px black solid; 
 
}
<table> 
 
    <tr> 
 
    <td>a1</td> 
 
    <td>a2</td> 
 
    <td>a3</td> 
 
    <td>a4</td> 
 
    </tr> 
 
    <tr> 
 
    <td>b1</td> 
 
    <td>b2</td> 
 
    <td>b3</td> 
 
    <td>b4</td> 
 
    </tr> 
 
    <tr> 
 
    <td>c1</td> 
 
    <td>c2</td> 
 
    <td>c3</td> 
 
    <td>c4</td> 
 
    </tr> 
 
    <tr> 
 
    <td>d1</td> 
 
    <td>d2</td> 
 
    <td>d3</td> 
 
    <td>d4</td> 
 
    </tr> 
 
</table>

Table 1 
+----+----+----+----+ 
| a1 | a2 | a3 | a4 | 
+----+----+----+----+ 
| b1 | b2 | b3 | b4 | 
+----+----+----+----+ 
| c1 | c2 | c3 | c4 | 
+----+----+----+----+ 
| d1 | d2 | d3 | d4 | 
+----+----+----+----+ 

Table 2 
+----+----+----+----+ 
| e1 | e2 | e3 | e4 | 
+----+----+----+----+ 
| f1 | f2 | f3 | f4 | 
+----+----+----+----+ 
| g1 | g2 | g3 | g4 | 
+----+----+----+----+ 
| h1 | h2 | h3 | h4 | 
+----+----+----+----+ 

And Other Tables ... 

我想獲得在塔的頂部印有細胞的細胞(即T R [1])。

輸出應該不具有第一原始..

第一輸出應爲:

單元B1具有頭A1

..

細胞G2具有標頭e2

等等。

我使用xidel:

xidel $site -e "//tr[position()>1]/td/concat('The cell ', ., $codeX)"

$codeX值應該是什麼?

感謝,

+0

如果你想使用第一行,那麼聽起來好像使用'// tr [1]/td'就是你想要的 –

+0

@MartinHonnen我想從第二行得到它,實際上我使用的是concat( )將它們一起打印。 – user37421

回答

0

要得到表頭TEX噸只得到第一個TR數據//tr[1]/td//tr[1]/th如果用於頭(預計)

按列文本嘗試獲得頭標記th此表上的XPath:https://www.w3schools.com/css/tryit.asp?filename=trycss_table_border

//th[count(//tr/td[text()='Griffin'])] 

邏輯是:找到的位置使用特定文本//tr/td[text()='Griffin'],通過使用count()函數。並找到th在這個位置

+0

我試圖用'../../tr[1]/td[count(./preceding-sibling::td)]' 來做到這一點,它沒有工作 – user37421

+0

你試過我的建議嗎?因爲你的定位器100%不正確 –

+0

我應該如何找到td元素內的文字?其實我有很多桌子和很多細胞,而且我的帖子也過於簡化了。 – user37421

2

Xidel支持XQuery 3.0,因此用於構建任務。

let $rows := //tr, 
    $header-cells := $rows[1]/td 
for $data-row in $rows[position() gt 1] 
for $cell at $pos in $data-row/td 
return $cell!('cell ' || . || ' has header ' || $header-cells[$pos]) 

不確定在命令行中該功能是否正常工作,但執行該任務。

相關問題