[英]XPath select on child nodes
I am currently trying to make a new xpath on a child node. 我目前正在尝试在子节点上创建新的xpath。 But it doesn't work. 但这是行不通的。
How can I archive this? 我该如何存档?
The idea is: 这个想法是:
xpath.select('//*[contains(@class, "product-grid__item")]', doc).forEach(node => {
node = node as Node;
xpath.select('/a /*[contains(@class, "desc-height")] /strong', node).forEach(z => {
z = z as Node;
console.log(z.textContent);
})
})
But the second request does not select on the nodes, it selects on the document. 但是第二个请求不在节点上选择,而是在文档上选择。
The expression 表达方式
/a/*[contains(@class, "desc-height")]/strong
is an absolute location path and it will evaluate equally from every context node. 是绝对位置路径,并且它将在每个上下文节点中平均评估。
You need a relative location path like 您需要类似的相对位置路径
a/*[contains(@class, "desc-height")]/strong
Note: some literature will always show you relative XPath expression starting with .
注意:一些文献总是会向您显示相对的XPath表达式.
abbreviate syntax like ./a/*[contains(@class, "desc-height")]/strong
. 缩写语法,例如./a/*[contains(@class, "desc-height")]/strong
。 That it's not needed. 不需要。 Some XPath engines not aligned with the specs might only parse this kind of expression just because. 一些不符合规范的XPath引擎可能仅因为这种原因而解析这种表达。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.