Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6100745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:25:15+00:00 2026-05-23T13:25:15+00:00

I didn’t found an explicit way to select all nodes that exist between two

  • 0

I didn’t found an explicit way to select all nodes that exist between two anchors (<a></a> tag pair) in an HTML file.

The first anchor has the following format:

<a href="file://START..."></a>

Second anchor:

<a href="file://END..."></a>

I’ve verified that both can be selected using starts-with (note that I’m using HTML Agility Pack):

HtmlNode n0 = html.DocumentNode.SelectSingleNode("//a[starts-with(@href,'file://START')]"));
HtmlNode n1 = html.DocumentNode.SelectSingleNode("//a[starts-with(@href,'file://END')]"));

With this in mind, and with my amateurish XPath skills, I wrote the following expression to get all tags between the two anchors:

html.DocumentNode.SelectNodes("//*[not(following-sibling::a[starts-with(@href,'file://START0')]) and not (preceding-sibling::a[starts-with(@href,'file://END0')])]");

This seems to work, but selects all HTML document!

I need to, for example for the following HTML fragment:

<html>
...

<a href="file://START0"></a>
<p>First nodes</p>
<p>First nodes
    <span>X</span>
</p>
<p>First nodes</p>
<a href="file://END0"></a>

...
</html>

remove both anchors, the three P (including of course the inner SPAN).

Any way to do this?

I don’t know if XPath 2.0 offers better ways to achieve this.

*EDIT (special case!) *

I should also handle the case where:

“Select tags between X and X’, where X is <p><a href="file://..."></a></p>“

So instead of:

<a href="file://START..."></a>
<!-- xhtml to be extracted -->
<a href="file://END..."></a>

I should handle also:

<p>
  <a href="file://START..."></a>
</p>
<!-- xhtml to be extracted -->

<p>
  <a href="file://END..."></a>
</p>

Thank you very much, again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:25:16+00:00Added an answer on May 23, 2026 at 1:25 pm

    Use this XPath 1.0 expression:

    //a[starts-with(@href,'file://START')]/following-sibling::node()
         [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
         =
          count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
         ]
    

    Or, use this XPath 2.0 expression:

        //a[starts-with(@href,'file://START')]/following-sibling::node()
      intersect
        //a[starts-with(@href,'file://END')]/preceding-sibling::node()
    

    The XPath 2.0 expression uses the XPath 2.0 intersect operator.

    The XPath 1.0 expression uses the Kayessian (after @Michael Kay) formula for the intersectioon of two node-sets:

    $ns1[count(.|$ns2) = count($ns2)]
    

    Verification with XSLT:

    This XSLT 1.0 transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
      <xsl:copy-of select=
      "    //a[starts-with(@href,'file://START')]/following-sibling::node()
             [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
             =
              count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
             ]
      "/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <html>...
        <a href="file://START0"></a>
        <p>First nodes</p>
        <p>First nodes    
            <span>X</span>
        </p>
        <p>First nodes</p>
        <a href="file://END0"></a>...
    </html>
    

    produces the wanted, correct result:

    <p>First nodes</p>
    <p>First nodes    
            <span>X</span>
    </p>
    <p>First nodes</p>
    

    This XSLT 2.0 transformation:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
      <xsl:copy-of select=
      " //a[starts-with(@href,'file://START')]/following-sibling::node()
       intersect
        //a[starts-with(@href,'file://END')]/preceding-sibling::node()
      "/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the same XML document (above) again produces exactly the wanted result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Didn't find how to do that. What I found was more or less on
I didn't attend PDC 2008, but I heard some news that C# 4.0 is
I didn't want to keep repeating the same select query, so I wrote this
I didn’t know about the <button> tag until today.
I didn't think I made any tweaks to my machine configuration lately but all
I didn't find a better way to phrase this question in the title. If
I didn't see anything on the documentation that allows me to see which files
(Didn't mean to create a new question, but revised the old one enough that
I didn't imagine that I would encounter radically new syntax in Java anymore at
I didn't see any questions that asked this previously so here goes: Once you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.