XPath with optional tbody element
Posted
by
Phrogz
on Stack Overflow
See other posts from Stack Overflow
or by Phrogz
Published on 2011-01-06T03:30:08Z
Indexed on
2011/01/06
3:53 UTC
Read the original article
Hit count: 399
As in this Stack Overflow answer imagine that you need to select a particular table and then all the rows of it. Due to the permissiveness of HTML, all three of the following are legal markup:
<table id="foo"><tr>...</tr></table>
<table id="foo"><tbody><tr>...</tr></tbody></table>
<table id="foo"><tr>...</tr><tbody><tr>...</tr></tbody></table>
You are worried about tables nested in tables, and so don't want to use an XPath like
table[@id="foo"]//tr
.
If you could specify your desired XPath as a regex, it might look something like:
table[@id="foo"](/tbody)?/tr
In general, how can you specify an XPath expression that allows an optional element in the hierarchy of a selector?
To be clear, I'm not trying to solve a real-world problem or select a specific element of a specific document. I'm asking for techniques to solve a class of problems.
© Stack Overflow or respective owner