我正在使用 HtmlAgilityPack 来获取过滤后的 DOM<h2>
和<h3>
节点,并使用 Xpath 1.0(来自我本周的 Xpath 1.0 速成课程)我需要获取<h3>
兄弟之间的 ' 数量(数量不同),<h2>
如下所示:
<div>
<h2>heading 1</h2>
<h3>sub 1.1</h3>
<h3>sub 1.2</h3>
<h2>heading 2</h2>
<h3>sub 2.1</h3>
<h2>heading 3</h2>
....
</div>
当我通过过滤的节点迭代(使用 C#)时,我想要在 a 之后和 next 之前的<h3>
's的确切数量。当我使用以下内容时,我得到了所有的's 结果。<h2>
<h2>
<h3>
int countH3 = n.SelectNodes("./preceding-sibling::h2[2]/following-sibling::h2[3]/preceding-sibling::h3").Count(); //the [position] is set dynamically
对于上面的节点结构,希望代码行的结果是:
countH3 = 1
但它是:
countH3 = 3
我发现了许多关于“兄弟节点之间的兄弟节点”的类似 SO 问题,并且必须感谢 @LarsH 在另一个/preceding::h3
返回 ALL的问题中的评论,<h3>'s
这有助于解释该问题。我想我可能需要使用节点集交集的 Kayessian 方法,但是当我包含. |
联合字符时会出现“无效令牌”错误,如下所示:
countH3 = n.SelectNodes("./h2[2]/following-sibling::h2[3]
[count(.|./h2[2]/following-sibling::h2[3]/preceding-sibling::h3)=
count(./h2[2]/following-sibling::h2[3]/preceding-sibling::h3)]").Count();
任何建议表示赞赏。