XPath 或,替代方案
我使用 CSharp、XPath 和 HTMLAgility Pack。我使用 XPath 字符串,例如:
"//table[3]/td[1]/span[2]/text() | //table[6]/td[1]/span[2]/text()"
"//table[8]/td[1]/span[2]/text() | //table[10]/td[1]/span[2]/text()"
差异仅在于表编号。是否可以使用其他 XPath 函数来替换 XPath 或 |
?
我实际上做了什么:使用第一个 XPath 字符串(其中有表号 3 和 6),我提取一个值。使用第二个 XPath 字符串(其中表编号为 8 和 10),我提取另一个值。
关于性能的其他问题 - XPath 字符串 //table[8]/td[1]/span[2]/text()
是否比使用 OR
//table[8]/td[1]/span[2]/text() | //表[10]/td[1]/span[2]/text()
?我问这个问题是因为我有很多很多值的 XPath 字符串,如果存在差异,这确实意味着我需要尝试其他方法。我现在无法进行测量,所以我问你这个问题来分享你的经验。
I use CSharp, XPath and HTMLAgility Pack. I use XPath strings such as:
"//table[3]/td[1]/span[2]/text() | //table[6]/td[1]/span[2]/text()"
"//table[8]/td[1]/span[2]/text() | //table[10]/td[1]/span[2]/text()"
The difference is only in table numbers. Is it possible to use some other XPath function to replace the XPath or |
?
What I actually do: With the first XPath string (where I have table numbers 3 & 6) I extract one value. With the second XPath string (where i have table numbers are 8 & 10) I extract another value.
And additional question about performance - is the XPath string //table[8]/td[1]/span[2]/text()
faster than the XPath string with OR
//table[8]/td[1]/span[2]/text() | //table[10]/td[1]/span[2]/text()
? I ask this because I have many many XPath strings for many many values and if there is a difference which really means I need to try something else. I can't do the measurement right now that's why I ask you this question to share your experience.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
//table[6]
看起来很奇怪。您确定您的意思不是(//table)[6]
吗? (第一个选择作为其父级的第六个子表的每个表;第二个选择文档中的第六个表。)我将假设后者。在 XPath 2.0 中,您可以写
在 1.0 中,这必须是
或者(在任一版本中)您可以写
如果不知道您正在使用什么 XPath 实现,则无法回答有关性能的问题。
Firstly,
//table[6]
looks odd. Are you sure you don't mean(//table)[6]
? (The first selects every table that is the 6th child of its parent; the second selects the sixth table in the document.) I will assume the latter.In XPath 2.0 you can write
In 1.0 that would have to be
Or (in either release) you could write
Your question about performance can't be answered without knowing what XPath implementation you are using.