2012-03-15 68 views
2

我有一個HTML文件,並希望找到第二TH內容的「Stn代碼」和第三TH內容的「路線號」表(多)之間。在php中獲取這個特定表格的XPATH表達式是什麼:這是什麼XPATH表達式

<table cellpadding="0" cellspacing="0" border="0"> 
    <tbody> 
     <tr class="heading_table_top"> 
      <th width="6%">SNo</th> 
      <th width="9%">Stn Code</th> 
      <th width="17%">Stn Name</th> 
      <th width="9%">Route No.</th> 
      <th width="9%">Arrival Time</th> 
      <th width="9%">Dep. Time</th> 
      <th width="15%">Halt Time (In Minutes)</th> 
      <th width="9%">Distance</th> 
      <th width="6%">Day</th> 
      <th width="20%">Remark</th> 

............................. .....

回答

1

這將找到具有指定的內容的表無論哪個th的內容包含:

//table[descendant::th="Stn Code" and descendant::th="Route No."] 

descendant axis包含上下文節點的後代;後代是孩子或孩子的孩子等;因此後代軸從不包含屬性或名稱空間節點。

demo

如果你想確保內容是在特定th元素,使用th[n]其中n是位置,例如爲測站代碼‘和第3 TH內容‘路由號’的「第二TH內容’。你會使用th[2]th[3]。位置是從1開始的。

//table[descendant::th[2]="Stn Code" and descendant::th[3]="Route No."] 

注意,在你的榜樣標記「路由號」是th[4]因此上述的XPath不會產生表中的結果節點。

Also have a look at this XPath tutorial

+0

謝謝,但在我的網頁,該表在許多表包裹和所有人都被退回。我只想要一個帶有這個文本的直系後裔 – user774250 2012-03-15 18:24:47

+0

這看起來工作:$ dom_xpath->查詢('//表// // tr [th [2] =「Stn代碼」和th [4] =「路由號」和th [7] =「暫停時間(分鐘)「]/parent :: *'); – user774250 2012-03-15 18:36:48

+0

@ user774250嘗試'// table [tbody/tr/th [2] ='Stn Code'and tbody/tr/th [4] ='Route No。'] – Gordon 2012-03-15 20:45:17

1

這應該工作:

//table/tbody/tr[th[2]='Stn Code'][th[3]='Route No.']/../.. 

編輯:我的第一選擇expession TR元件,這應該選擇表格。