我们需要HTML注释之间读取节点:的XPath选择多次
<html>
<!-- comment 1 -->
<div>some text</div>
<div><p>Some more elements</p></div>
<!-- end content -->
<!-- comment 2 -->
<div>some text</div>
<div><p>Some more elements</p>
<!-- end content -->
</div>
</html>
我尝试使用下面的XPath:
//*[preceding-sibling::comment()[contains(., 'comment 1')]][following-sibling::comment()[contains(., 'end content')]]
它工作正常的第一个评论即注释1,但不针对第二条评论 以下是XPath的相同内容
//*[preceding-sibling::comment()[contains(., 'comment 2')]][following-sibling::comment()[contains(., 'end content')]]
I我使用HTML敏捷性包与下面的代码:
var nodes = document.SelectNodes("//*[preceding-sibling::comment()[contains(., 'comment 1')]][following-sibling::comment()[contains(., 'InstanceEndEditable')]]");
string allHtml = nodes[0].OuterHtml;
如果我改变“注释1”在上面的代码“注释2”,那么它是不会放弃任何结果。
请注意,您的HTML Agility Pack示例代码中的XPath包含一个额外的约束('[contains(。,'InstanceEndEditable')]]'),您可能打算编辑 - 它与您的例。 – kjhughes 2014-10-16 13:25:00
我回滚了源代码的更改,因为当您以该方式重新排列源代码时,它完全更改了原始问题。完成后请随时提出一个新问题。谢谢。 – kjhughes 2014-10-16 13:35:04