dongzhijing8202 2018-04-05 21:02
浏览 82
已采纳

Php Dom - 表中的特定列

I have trouble calculating a specific column with Dom Document and Xpath.

This is how the source file looks like:

    already some other tables and then...

        <table><hr><tr><td><table>
<td align="center" colspan="1"><u><b>Contracts</b></u></td>
<tr><th>pos</th><th>player</th><th>age</th><th>year 1</th><th>year 2</th><th>year 3</th><th>year 4</th><th>year 5</th><th>year 6</th></tr>
<tr><td CLASS=tdp>PG</td><td CLASS=tdp><a href="JamesHarden7.htm">James Harden                    </a></td><td>27</td><td>20.00</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>PG</td><td CLASS=tdp><a href="TerryRozier1.htm">Terry Rozier                    </a></td><td>22</td><td>1.10</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>SG</td><td CLASS=tdp><a href="DannyGreen6.htm">Danny Green                     </a></td><td>29</td><td>2.60</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>SG</td><td CLASS=tdp><a href="MarcoBelinelli3.htm">Marco Belinelli                 </a></td><td>30</td><td>1.50</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>SF</td><td CLASS=tdp><a href="LuolDeng12.htm">Luol Deng                       </a></td><td>31</td><td>1.75</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>SF</td><td CLASS=tdp><a href="JeremyEvans9.htm">Jeremy Evans                    </a></td><td>28</td><td>7.50</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>PF</td><td CLASS=tdp><a href="JeffWithey8.htm">Jeff Withey                     </a></td><td>26</td><td>6.25</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>PF</td><td CLASS=tdp><a href="LavoyAllen11.htm">Lavoy Allen                     </a></td><td>27</td><td>1.50</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp> C</td><td CLASS=tdp><a href="JonasValanciunas10.htm">Jonas Valanciunas               </a></td><td>24</td><td>12.75</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp> C</td><td CLASS=tdp><a href="RyanHollins4.htm">Ryan Hollins                    </a></td><td>31</td><td>1.50</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>SF</td><td CLASS=tdp><a href="K.J.McDaniels2.htm">K.J. McDaniels                  </a></td><td>23</td><td>1.50</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>PG</td><td CLASS=tdp><a href="BrianteWeber5.htm">Briante Weber                   </a></td><td>24</td><td>4.35</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td CLASS=tdp>SF</td><td CLASS=tdp><a href="NicolasBrussino13.htm">Nicolas Brussino                </a></td><td>23</td><td>1.00</td><td></td><td></td><td></td><td></td><td></td></tr>
</table></td><td><table>
    ...

I worked with this code, similar to one I've found here, but I always get "0" as result.

$doc = new DOMDocument;
$doc->loadHTML('URL');

$xpath = new DOMXPath($doc);

// sum of cells of the sixth table (contracts), in the fourth column (year1), skipping the first row (ignore Year 1)
print $xpath->evaluate('sum(//table[6]//tr[position() > 1]/td[4])');
  • 写回答

1条回答 默认 最新

  • dqppv86022 2018-04-05 21:24
    关注

    It can be difficult when using terms like table[6] in XPath as this is so dependant on the overall document structure. It's better if you can pick up on something like <b>Contracts</b> as part of the table your interested in and search for that table.

    So you could try...

    print $xpath->evaluate('sum(//table[td/u/b/.="Contracts"]/tr[position() > 1]/td[4])');
    

    Update:

    To help work out what it's doing you can break it down to levels and see what it's returning. To check if it's finding the table, use...

    $table = $xpath->query('//table[td/u/b="Contracts"]');
    echo $doc->saveHTML($table[0]);
    

    Then add onto it to see where it's failing. One of the big difficulties can be that as your using HTML, is a constant problem of bad HTML gets converted into XML and it can loose some of it's structure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里