dongzhijing8202
dongzhijing8202
2018-04-05 21:02

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 dqppv86022 3年前

    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.

    点赞 评论 复制链接分享

相关推荐