0

我问了一个关于在 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,现在因为class="circle"标签本身就是单独的节点(不在class="subsection"节点内),我不能只在特定小节内获取文章,它会获取第一小节中的所有文章,然后在第二小节中它还从第三小节中获取文章,它只能在最后一个小节,因为它没有更多的小节。

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

4

1 回答 1

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"

我希望你已经抓住了这个想法。

于 2013-10-28T20:35:36.593 回答