问题
I have asked a question about Traversing back to parent node in xpath
i got expected reply, however i have one more doubt with the further development. With below 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>
with the help of @Alex
now i can get all the sections and subsections. then i wanted the articles inside the subsection, i tried that myself and received expected output, however my problem is now after the <li class="subsection">
tag we have individual <li class="circle">
tag which contains the information of the article inside anchor
tag, now as the class="circle"
tag is itself the individual node (not inside the class="subsection"
node), i cannot just gets article inside the particular subsection, it fetches all the articles in first subsection and then, in second sub-section it also fetches the articles from the third sub-section, it only works fine in the last sub-section as it does not have any more sub-sections.
Can anyone tell me how shall i limit the articles within the particular sub-section, i mean when it gets the class="subsection"
tag, it should stop fetching for articles further for that subsection, and start fetching for another sub-section.
回答1:
You could use something like:
//ul/ul[preceding::li[@class='section' and .=\"BROADCASTING\"]]/li[@class='subsection' and .=\"\"]/following::li[@class='circle' and following::li[@class='subsection' and .=\"Null\"]]/a
to get all the anchors between <li class='subsection'></li>
and <li class='subsection'>Null</li>
. To get links between <li class='subsection'>Null</li>
and <li class='subsection'>PEOPLE'S CHOICE</li>
you need to replace necessary values here: li[@class='subsection' and .=\"{...}\"
.
To get anchors under the last subsection, the next expression will be enough:
//ul/ul[preceding::li[@class='section' and .=\"BROADCASTING\"]]/li[@class='subsection' and .=\"PEOPLE'S CHOICE\"]/following::li[@class='circle']/a"
.
I hope you've caught the idea.
来源:https://stackoverflow.com/questions/19616484/traversing-back-to-the-previous-node-using-xpath-in-asp-net