在asp.net中使用XPath遍历回到上一个节点

时间:2013-10-27 09:24:23

标签: asp.net xpath html-agility-pack

我问了一个关于Traversing back to parent node in xpath

的问题

我得到了预期的答复,但是我对进一步的发展还有一个疑问。使用以下HTML:

<ul><li class="section">BROADCASTING</li>
<ul>
<li class="subsection"></li>
<li class="circle"><a href="/article/95242-STATION_BREAK.php">STATION BREAK</a></li>
<li class="circle"><a href="/article/98142-Labor_pains_hunger_pangs.php">Labor pains, hunger pangs</a></li>
<li class="circle"><a href="/article/101509-Wake_up_call_for_Dream_Team.php">Wake-up call for Dream Team</a></li>
<li class="circle"><a href="/article/136139-News_crew_turns_rescuer.php">News crew turns rescuer</a></li>
<li class="circle"><a href="/article/136140-Chopper_safety_had_been_challenged.php">Chopper safety had been challenged</a></li>
<li class="circle"><a href="/article/136142-Nielsen_adds_Dayton_.php">Nielsen adds Dayton..</a></li>
<li class="circle"><a href="/article/136143-Mondale_watch.php">Mondale watch</a></li>
<li class="circle"><a href="/article/136144-Those_70s_clearances.php">Those 70s clearances</a></li>
<li class="circle"><a href="/article/136145-Oscar_goes_to_ABC.php">Oscar goes to ABC</a></li>
<li class="circle"><a href="/article/136146-Hearst_Argyle_gives_a_green_light.php">Hearst-Argyle gives a green light</a></li>
<li class="circle"><a href="/article/136147-Finding_Geena_Davis.php">Finding Geena Davis</a></li>
<li class="circle"><a href="/article/136148-Syndication_Wrap_up.php">Syndication Wrap-up</a></li>
<li class="circle"><a href="/article/136149-CBS_TV_news_pioneer_dies_at_86.php">CBS TV news pioneer dies at 86</a></li>
<li class="circle"><a href="/article/136150-New_York_anchor_remembered.php">New York anchor remembered</a></li>
<li class="circle"><a href="/article/136151-News_sharing_in_West_Virginia.php">News sharing in West Virginia</a></li>
<li class="circle"><a href="/article/136152-News_dropping_in_Orlando.php">News dropping in Orlando</a></li>
<li class="subsection">Null</li>
<li class="circle"><a href="/article/136141-GET_WITH_THE_PROGRAM.php">GET WITH THE PROGRAM</a></li>
<li class="subsection">PEOPLE'S CHOICE</li>
<li class="circle"><a href="/article/97423-Syndication_as_branding.php">Syndication as branding</a></li>
</ul>
</ul>

@Alex 的帮助下,现在我可以获得所有部分和子部分。然后我想要小节内的文章,我自己尝试并收到了预期的输出,但是我的问题现在在<li class="subsection">标签之后我们有个人<li class="circle">标签,其中包含anchor内文章的信息1}}标签,现在因为class="circle"标签本身就是单个节点(不在class="subsection"节点内),我不能只在特定小节内获取文章,它会获取第一小节中的所有文章,然后,在第二小节中,它也从第三个子节获取文章,它只在最后一个小节中正常工作,因为它没有任何更多的子节。

任何人都可以告诉我如何限制特定子部分中的文章,我的意思是当它获得class="subsection"标签时,它应该停止为该子部分进一步获取文章,并开始获取另一个子部分-section。

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

//ul/ul[preceding::li[@class='section' and .=\"BROADCASTING\"]]/li[@class='subsection' and .=\"\"]/following::li[@class='circle' and following::li[@class='subsection' and .=\"Null\"]]/a

获取<li class='subsection'></li><li class='subsection'>Null</li>之间的所有锚点。要获取<li class='subsection'>Null</li><li class='subsection'>PEOPLE'S CHOICE</li>之间的链接,您需要在此处替换必要的值:li[@class='subsection' and .=\"{...}\"

要获得最后一个小节下的锚点,下一个表达式就足够了: //ul/ul[preceding::li[@class='section' and .=\"BROADCASTING\"]]/li[@class='subsection' and .=\"PEOPLE'S CHOICE\"]/following::li[@class='circle']/a"

我希望你已经抓住了这个主意。