Xpath 在锚点帮助中选择以下(数字)同级
如果我没有解释清楚,请提前抱歉。我会尽力澄清我正在尝试做的事情。言归正传...假设页面上有一系列数字(仅用空格分隔),其中括号表示您当前所在的页面。
它在页面上看起来像这样:
[1] 2 3
HTML 看起来像这样:
<tr>
<td>
[<a href='link1.php'>1</a>] <a href='link2.php'>2</a> <a href='link3.php'>3</a>
</td>
</tr>
我只是想根据当前页码选择下一个页码。我想我需要使用某种形式的 follow-sibling,但我想出的只是 //tr/td/a/following-sibling::a[1],这显然是不正确的。正如预期的那样,它在第 1 页上时选择 2,但在第 2 页上时不选择 3。我会尝试使用 [text()[contains(.,'[')]] 来选择当前页面,但括号位于锚点之外而不是内部。哎呀!?
如果您能解释思考过程以及解决方案,而不是仅仅粘贴答案,我们将不胜感激。期待您的帮助。
Sorry in advance if I don't explain this clearly. I will try my best to clarify what I am trying to do. Without further ado...Suppose you have a series of numbers on a page (separated by only a space) where brackets indicate the current page you are on.
It looks like this on the page:
[1] 2 3
The HTML looks like this:
<tr>
<td>
[<a href='link1.php'>1</a>] <a href='link2.php'>2</a> <a href='link3.php'>3</a>
</td>
</tr>
I am simply trying to select the next page number based on the current. I imagine I need to use some form of following-sibling but all I have come up with is //tr/td/a/following-sibling::a[1], which is obviously incorrect. It selects 2 when on page 1 but not 3 when on page 2 as expected. I would have tried to use a[text()[contains(.,'[')]] to select the current-page but the brackets are outside the anchor as opposed to inside. Eek!?
It would be much appreciated if you could explain the thought process along with your solution instead of just pasting the answer. Looking forward to your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
2 之后的下一页将通过以下方式找到:
首先,我们根据字符串内容选择当前页面
//tr/td/a[.='2']
,然后从那里选择第一个跟随兄弟姐妹。为了在不知道当前页面的情况下执行此操作,我们将选择“]”后面的 a 元素。
如果没有当前页面,我们将默认使用第一个 a 元素。
The next page after 2, will be found by:
First we select the current page based on its string content
//tr/td/a[.='2']
, and from there select the first following-sibling.To do this without knowing the current page, we will instead select the a element following the "]".
And in case that there is no current page, we will default to the first a element.