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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…