2012-03-15 65 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元件,这应该选择表格。