Abbas
Abbas

Reputation: 5044

Traversing back to parent node in xpath

Below is my 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>

Now i want to seperate sections and subsections and get the url inside subsections. i have go to the each sections

Now as per the HTML structure, section is in seperate li which closes before subsection opens, so once i have reached section, i have to move back to ul and then have to continue to get subsection

However when i try ../ to go back to the ul (the one before section), it says ivalid token

This is what i tried to come back to the parent one.

HtmlNodeCollection sections = doc.DocumentNode.SelectNodes("//ul/li[@class='section'][contains(text(), 'BROADCASTING')]../ul");

but this does not works

Can anyone tell me where i am going wrong, it will be a kind help.

Upvotes: 0

Views: 473

Answers (1)

Oleks
Oleks

Reputation: 32333

You need to change your xpath expression a bit:

var subsections = doc.DocumentNode.SelectNodes("//ul/ul[preceding-sibling::li[@class='section' and .='BROADCASTING']]/li[@class='subsection']");

It searches for <ul> inside <ul> that has <li class="section">BROADCASTING</li> as a previous sibling, and then searches for <li class="subsection"> inside it.

So the code above selects all the subsections under BROADCASTING section.

Hope this helps.

Upvotes: 1

Related Questions