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

  • Home
  • SEARCH
  • 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 7443735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:22:46+00:00 2026-05-29T11:22:46+00:00

I’ve been writing some code that extracts the main textual content from web pages.

  • 0

I’ve been writing some code that extracts the main textual content from web pages. One strategy that’s been useful is to locate the first paragraph of content, then select all of the following sibling elements up to, but not including, the first one that isn’t a p, ul, ol, or blockquote element. In Perl, the code looks something like this:

my ($firstpara) = $document->findnodes('//p[whatever]');
my @content = ($firstpara);
for my $sibling ($firstpara->findnodes('following-sibling::*')) {
    last if $sibling->tag !~ /^(?:p|ol|ul|blockquote)\z/;
    push @content, $sibling;
}

This isn’t too bad, but it would be cool to be able to get the nodes I want using only XPath, so I could write something like this instead:

my ($firstpara) = $document->findnodes('//p[whatever]');
my @content = ($firstpara, $firstpara->findnodes('<query>'));

I’ve done a lot of experimentation, but haven’t been able to figure out how to write that last query. The closest to a valid-looking solution I’ve been able to find is something like:

$firstpara->findnodes('following-sibling::*[position() < $EXPR]');

…where $EXPR is some expression that returns the position of the next sibling whose tag is not p, ul, ol, or blockquote, but I haven’t been able to work out if such an expression is expressible in XPath.

Is there any way to do what I’ve described in XPath?

Example:

Suppose my document looks like this:

<h1>Header</h1>
<p>Paragraph 1</p>
<p id="first">Paragraph 2</p>
<p>Paragraph 3</p>
<ul><li>Item 1</li><li>Item 2</li></ul>
<p>Paragraph 4</p>
<hr>
<p>Paragraph 5</p>
<blockquote>Blockquote 1</blockquote>
...

I have a reference to the <p> element with id first. I’m after an XPath expression, using that first element as the content node, that will give me the following siblings Paragraph 3, the unordered list, and Paragraph 4. The <hr> element is not among those I want (<p>, <ul>, <ol>, and <blockquote>), so that element and all siblings after that should not be part of the returned node set.

  • 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-29T11:22:47+00:00Added an answer on May 29, 2026 at 11:22 am

    As the OP explained, he wants:

    all of the following sibling elements up to, but not including, the
    first one that isn’t a p, ul, ol, or blockquote element

    I. XPath 1.0 solution:

    The nodes that are wanted are the intersection of two nodesets:

    1. All elements that are following siblings of the p with id with value 'first'.

    2. All elements that are preceding siblings of hr.

    To find this with XPath 1.0 we use the Kayessian formula for nodeset intersection:

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

    The above XPath expression selects all nodes that belong both to the node-set $ns1 and to the node-set $ns2.

    Let $vP1 is defined as /*/p[@id='first'].

    Let $vFirstNotInRange is:

       $vP1/following-sibling::*
        [not(self::p or self::ul
            or self::ol or self::blockquote)
        ] [1]
    

    This selects the first unwanted node (in this case hr), or more precisely: the first element that is a following sibling of $vP1 and that is not a p, a ul, a ol or a blockquote.

    Then the two node-sets we want to intersect are all following siblings of $vP1 and all preceding siblings of $vFirstNotInRange:

    Let us denote with $vFollowingP1 the first node-set — this is:

    $vP1/following-sibling::*
    

    And let us denote with $vPreceedingNotInRange the second node-set — this is:

    $vFirstNotInRange/preceding-sibling::*
    

    Finally we substitute in the Kayessina formula $ns1 with $vPreceedingNotInRange and $ns2 with $vFollowingP1. The reult of these substitutions selects exactly the wanted nodes:

    $vPreceedingNotInRange
             [count(.|$vFollowingP1)
             =
              count($vFollowingP1)
             ]
    

    If we substitute all variables until we get an expression that doesn’t contain any variables, we get:

       /*/p[@id='first']/following-sibling::*
         [not(self::p or self::ul
             or self::ol or self::blockquote
              )
         ] [1]
            /preceding-sibling::*
              [count(.| /*/p[@id='first']/following-sibling::*)
              =
               count(/*/p[@id='first']/following-sibling::*)
              ]
    

    This expression selects exactly the wanted nodes.

    Here is an XSLT – based verification:

    <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:variable name="vP1" select="/*/p[@id='first']"/>
    
     <xsl:variable name="vFirstNotInRange" select=
      "$vP1/following-sibling::*
        [not(self::p or self::ul
            or self::ol or self::blockquote)
        ] [1]"/>
    
     <xsl:variable name="vFollowingP1"
          select="$vP1/following-sibling::*"/>
    
     <xsl:variable name="vPreceedingNotInRange"
          select="$vFirstNotInRange/preceding-sibling::*"/>
    
     <xsl:template match="/">
      <xsl:copy-of select=
      "$vPreceedingNotInRange
       [count(.|$vFollowingP1)
       =
        count($vFollowingP1)
       ]"/>
    ================
    
      <xsl:copy-of select=
      "/*/p[@id='first']/following-sibling::*
         [not(self::p or self::ul
             or self::ol or self::blockquote
              )
         ] [1]
            /preceding-sibling::*
              [count(.| /*/p[@id='first']/following-sibling::*)
              =
               count(/*/p[@id='first']/following-sibling::*)
              ]
    
      "/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document (the provided non-wellformed XML fragment — corrected and wrapped in order to be made wellformed):

    <html>
        <h1>Header</h1>
        <p>Paragraph 1</p>
        <p id="first">Paragraph 2</p>
        <p>Paragraph 3</p>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
        </ul>
        <p>Paragraph 4</p>
        <hr/>
        <p>Paragraph 5</p>
        <blockquote>Blockquote 1</blockquote>
    </html>
    

    the two XPath expressions (one with variables and one with all variables substituted) are evaluated and the wanted, correct selected nodes output:

    <p>Paragraph 3</p>
    <ul>
       <li>Item 1</li>
       <li>Item 2</li>
    </ul>
    <p>Paragraph 4</p>
    ================
    
      <p>Paragraph 3</p>
    <ul>
       <li>Item 1</li>
       <li>Item 2</li>
    </ul>
    <p>Paragraph 4</p>
    

    II. XPath 2.0 solution:

    $vFirstNotInRange/preceding-sibling::*
                                  [. >> $vP1]
    

    This selects any preceding sibling of $vFirstNotInRange that is also following $vP1 and selects the same wanted nodes:

    <p>Paragraph 3</p>
    <ul>
       <li>Item 1</li>
       <li>Item 2</li>
    </ul>
    <p>Paragraph 4</p>
    

    Explanation: Here we use the XPath 2.0 “follows” operator >>.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

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.