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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:55:48+00:00 2026-06-13T05:55:48+00:00

Suppose I have an HTML table with the following rows, … <tr> <th title=Library

  • 0

Suppose I have an HTML table with the following rows,

...
<tr>
  <th title="Library of Quintessential Memes">LQM:</th>
  <td>
    <a href="docs/lqm.html"><b>Intro</b></a>
    <a href="P/P79/">79</a>
    <a href="P/P80/">80</a>
    <a href="P/P81/">81</a>
    <a href="P/P82/">82</a>
  </td>
</tr>
<tr>
  <th title="Library of Boring Books">LBB:</th>
  <td>
    <a href="docs/lbb.html"><b>Intro</b></a>
    <a href="R/R80/">80</a>
    <a href="R/R81/">81</a>
    <a href="R/R82/">82</a>
    <a href="R/R83/">83</a>
    <a href="R/R84/">84</a>
  </td>
</tr>
...

I would like to select all <a> elements in a <td> element whose associated <th>‘s text is in a small set of fixed titles (e.g. LQM, LBR, and RTT). How can I formulate this as an XPath query?

EDIT: I am using Scrapy, a Python scraping toolkit, so if it is easier to phrase this query as a set of smaller queries, I would be more than happy to use that. For example, if I could select all <tr> elements whose first <th> child matches a regex, then select all <a> descendants of the remaining <tr> elements, that would be splendid.

  • 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-13T05:55:50+00:00Added an answer on June 13, 2026 at 5:55 am

    The following XPath will work:

    //a[contains(',LQM:,LBR:,RTT:,',
                 concat(',', ancestor::td/preceding-sibling::th, ','))]
    

    This can theoretically get some false positives (if your codes contained commas).

    A stricter way to say it would be:

    //a[ancestor::td/preceding-sibling::th[.='LQM:']]
    |//a[ancestor::td/preceding-sibling::th[.='LBR:']]
    |//a[ancestor::td/preceding-sibling::th[.='RTT:']]
    

    I tested this by adding a <table> tag around your input and applying the following XSL transform:

    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:template match="/">
            <xsl:for-each select="//a[ancestor::td/preceding-sibling::th[.='LQM:']]
                                      |//a[ancestor::td/preceding-sibling::th[.='LBR:']]
                                      |//a[ancestor::td/preceding-sibling::th[.='RTT:']]">
                <xsl:text>
    </xsl:text>
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </xsl:template>
    
    </xsl:transform>
    

    It produces the following output:

    <a href="docs/lqm.html"><b>Intro</b></a>
    <a href="P/P79/">79</a>
    <a href="P/P80/">80</a>
    <a href="P/P81/">81</a>
    <a href="P/P82/">82</a>
    

    Of course, if you are using XSL, then you might find this construction more readable:

    <xsl:for-each select="//a">
        <xsl:variable name="header" select="ancestor::td/preceding-sibling::th"/>
    
        <xsl:if test="$header='LQM:' or $header = 'LBR:' or $header = 'RTT:'">
            <xsl:text>
            </xsl:text>
            <xsl:copy-of select="."/>
    
        </xsl:if>
    </xsl:for-each>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following html: var testString = ' <span id='id'> <div> <table>
A newbie question,really. Suppose I have a html table like this: <div id=div1> <table
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input
Suppose I have the following HTML: <div id=test> <span style=display:inline;>foo</span> <span style=display:none;>bar</span> <span style=display:inline;>baz</span>
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Suppose on my HTML page I have many href s to a link. Using
Suppose I have the HTML code as below <table width=600 border=0 cellpadding=0 cellspacing=0 align=center
suppose I have a nested tables in html like this <table> <tr> <td><table> <tr>
Suppose I have the following MySQL table result: ID price ------------- 1 10 2
I have made a HTML table. Suppose I have a link, for example, link

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.