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 7725635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:59:17+00:00 2026-06-01T04:59:17+00:00

I have a HTML/XML document similar to the following. There can be one or

  • 0

I have a HTML/XML document similar to the following. There can be one or more ‘tr’ of the same colour before switching to the other colour in an arbitrarily repeating pattern. This is an example:

<tr class='red'></tr>
<tr class='blue'></tr>
<tr class='red'></tr>
<tr class='red'></tr>
<tr class='red'></tr>
<tr class='blue'></tr>
<tr class='blue'></tr>
<tr class='red'></tr>
<tr class='red'></tr>
<tr class='blue'></tr>

What I am looking for is an XPath (1.0) expression which, starting from the first ‘tr’ in any colour ‘block’ (note that there is no markup indicating these blocks, only alterations in the colour), selects the following subsequent ‘tr’s within that block only.

I have tried the following expression

./following-sibling::tr[@class=preceding-sibling::tr[1]/@class]

but this also selects the second+ ‘tr’s of subsequent blocks. I feel like I’m close to what I need, but can’t quite manage it.

Thanks in advance.

Edit: The desired output is a nodeset containing the subsequent ‘tr’s within the block (and only that block).

  • 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-06-01T04:59:18+00:00Added an answer on June 1, 2026 at 4:59 am

    This XPath 1.0 expression selects the first “block” of blue tr elements:

          (/*/tr[@class='blue'][1] | /*/tr[@class='blue'][1]/following-sibling::tr)
            [count(. | /*/tr[@class='blue'][1]
                              /following-sibling::tr
                                        [not(@class='blue')][1]
                                           /preceding-sibling::*
                   )
            =
             count(/*/tr[@class='blue'][1]
                              /following-sibling::tr
                                        [not(@class='blue')][1]
                                           /preceding-sibling::*
             )
             ]
    

    Explanation:

    Using the wellknown Kayessian formula for node-set intersection:

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

    This XPath expression selects exactly the nodes that belong to both the node-set $ns1 and the node-set $ns2.

    In this particular case we simply substitute $ns1 and $ns2 with their appropriate specific XPath expressions — one is the first blue tr and all of its following siblings, the other is the first non-blue tr following the first blue tr and all of its preceding siblings. The intersection of these two node-sets is exactly the wanted first block of blue trs.

    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:template match="node()|@*">
      <xsl:copy-of select=
      "(/*/tr[@class='blue'][1] | /*/tr[@class='blue'][1]/following-sibling::tr)
                [count(. | /*/tr[@class='blue'][1]
                                  /following-sibling::tr
                                            [not(@class='blue')][1]
                                               /preceding-sibling::*
                       )
                =
                 count(/*/tr[@class='blue'][1]
                                  /following-sibling::tr
                                            [not(@class='blue')][1]
                                               /preceding-sibling::*
                     )
                 ]
      "/>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the following XML document:

    <t>
        <tr class='red'></tr>
        <tr class='red'></tr>
        <tr class='red'></tr>
        <tr class='red'></tr>
        <tr class='blue'></tr>
        <tr class='blue'></tr>
        <tr class='red'></tr>
        <tr class='red'></tr>
        <tr class='blue'></tr>
    </t>
    

    the XPath expression is evaluated and the selected nodes are copied to the output:

    <tr class="blue"/>
    <tr class="blue"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following at the top of my document: <html class=js lang=en xml:lang=en
I have a string from an xml document: <title type=html> Is there a way
I have a single xml document (data.xml), which I display as HTML using an
I am using MSXML3 and have loaded an xml document which is a HTML
I have XML tag that has the content which is HTML document. <xml-tag> <!--
Let's say i have a full html-document as XML-input. How would the XSLT-file look
I have an XML document with no CDATA tags and embedded HTML. How do
I have the following XML document <?mso-infoPathSolution name=urn:schemas-microsoft-com:office:infopath:TCM-TCP:-myXSD-2012-02-20T14-33-22 solutionVersion=1.0.0.65 productVersion=14.0.0.0 PIVersion=1.0.0.0 href=https://devcoop.oceanspray.com/sites/TCM2/FormServerTemplates/TCM-TCP.xsn?> <?mso-application progid=InfoPath.Document
I have an HTML document stored in memory as an Linq-to-XML object tree. How
I have the following XML document <?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009' gd:etag='W/DEAERH47eCl7ImA9WhZTFEQ.'><id>http://code.google.com/feeds/issues/p/chromium/issues/full/921</id><published>2008-09-03T22:51:22.000Z</published><updated>2011-03-19T01:05:05.000Z</updated><title>Incorrect rendering</title><content

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.