从具有xpath的特定类的表中获取最后的td

时间:2019-03-11 17:29:15

标签: html xpath

我有以下html代码:

<table class="datepicker-days">
  <tbody>
    <tr data-tryxpath-element="0">
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td class="day  voc-enabled" data-day="01" data-month="03" data-year="2019" data-date="2019-03-01">01</td>
      <td class="day  voc-enabled" data-day="02" data-month="03" data-year="2019" data-date="2019-03-02">02</td>
      <td class="day  voc-enabled" data-day="03" data-month="03" data-year="2019" data-date="2019-03-03">03</td>
   </tr>
   <tr data-tryxpath-element="1">
       <td class="day  voc-enabled" data-day="04" data-month="03" data-year="2019" data-date="2019-03-04">04</td>
       <td class="day  voc-enabled" data-day="05" data-month="03" data-year="2019" data-date="2019-03-05">05</td>
       <td class="day  voc-enabled" data-day="06" data-month="03" data-year="2019" data-date="2019-03-06">06</td>
       <td class="day  voc-enabled" data-day="07" data-month="03" data-year="2019" data-date="2019-03-07">07</td>
       <td class="day  voc-enabled" data-day="08" data-month="03" data-year="2019" data-date="2019-03-08">08</td>
       <td class="day  voc-enabled" data-day="09" data-month="03" data-year="2019" data-date="2019-03-09">09</td>
       <td class="day  voc-enabled" data-day="10" data-month="03" data-year="2019" data-date="2019-03-10">10</td>
   </tr>
   <tr data-tryxpath-element="2">
       <td class="day  voc-enabled" data-day="11" data-month="03" data-year="2019" data-date="2019-03-11">11</td>
       <td class="day  voc-disabled" data-day="12" data-month="03" data-year="2019" data-date="2019-03-12">12</td>
       <td class="day  voc-disabled" data-day="13" data-month="03" data-year="2019" data-date="2019-03-13">13</td>
       <td class="day  voc-disabled" data-day="14" data-month="03" data-year="2019" data-date="2019-03-14">14</td>
       <td class="day  voc-disabled" data-day="15" data-month="03" data-year="2019" data-date="2019-03-15">15</td>
       <td class="day  voc-disabled" data-day="16" data-month="03" data-year="2019" data-date="2019-03-16">16</td>
       <td class="day  voc-disabled" data-day="17" data-month="03" data-year="2019" data-date="2019-03-17">17</td>
   </tr>
   <tr>
       <td class="day  voc-disabled" data-day="18" data-month="03" data-year="2019" data-date="2019-03-18">18</td>
       <td class="day  voc-disabled" data-day="19" data-month="03" data-year="2019" data-date="2019-03-19">19</td>
       <td class="day  voc-disabled" data-day="20" data-month="03" data-year="2019" data-date="2019-03-20">20</td>
       <td class="day  voc-disabled" data-day="21" data-month="03" data-year="2019" data-date="2019-03-21">21</td>
       <td class="day  voc-disabled" data-day="22" data-month="03" data-year="2019" data-date="2019-03-22">22</td>
       <td class="day  voc-disabled" data-day="23" data-month="03" data-year="2019" data-date="2019-03-23">23</td>
       <td class="day  voc-disabled" data-day="24" data-month="03" data-year="2019" data-date="2019-03-24">24</td>
    </tr>
    ...
</tbody>

我需要使用带有xpath的voc-enabled元素获取最后的td。在此示例中,只有文本为“ 11”的td。我最好的替代方法如下:

//table[@class='datepicker-days']//td[contains(@class,'voc-enabled')][last()]

但是此xpath会获得3个元素,最后一个td在每个tr元素中都具有“ voc-enabled”类。

当然,此表是动态的,我总是需要独立地选择最后一天。

有人可以帮我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

由于operator precedence in XSLT,您需要将表达式放在方括号中:

(//table[@class='datepicker-days']//td[contains(@class,'voc-enabled')])[last()]

错误是[]括号的优先级高于//,因此在[last()]之前应用了//