I understand your actual problem as: Find all links that belong to a given category. For doing so, find the category, and then retrieve all elements before the next category.
You might remove the newlines if you prefer, I added them for readability.
//tr[td/h3="Energy"]/(self::tr, following-sibling::tr[
. << //tr[td/h3="Energy"]/following-sibling::tr[td/h3][1]
])
If you do not have an XPath 2.0 compatible processor, you cannot use the <<
operator which test for node order (the current node must precede the next category). An XPath 1.0 solution is even slightly shorter, but in my opinion worse in readability:
//tr[td/h3="Energy"] | //tr[td/h3="Energy"]/following-sibling::tr[
./preceding-sibling::tr[td/h3][1][td/h3="Energy"] and not(td/h3)
]
Both queries will select all nodes of a category; to count them wrap them into count(...)
.