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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:20:16+00:00 2026-06-09T11:20:16+00:00

I have an HTML document that I would like to query using C# and

  • 0

I have an HTML document that I would like to query using C# and XPath. What I am searching for is an XPath expression – not XSLT, C#, PHP or any other language-specific code samples. Any help will be highly appreciated but the XPath expression is all I need :).

<tr>
  <td>
    <p>
      <span>text</span>
    </p>
  </td>
  <td>
    <p>
      <span>text</span>
    </p>
  </td>
</tr>
<tr>
  <td>
    <p>
      <span>This text is static and will never change</span>
    </p>
  </td>
  <td>
    <p>
      <span>Bla bla bla .... more bla bla bla</span>
    </p>
  </td>
</tr>
<tr>
  <td>
    <p>
      <span>text</span>
    </p>
  </td>
  <td>
    <p>
      <span>text</span>
    </p>
  </td>
</tr>

The XPath expression that I am looking for will extract the text that is currently represented by the string instance “Bla bla bla …. more bla bla bla”. This text will vary from HTML document to HTML document but one string is ALWAYS the same. In this case that string is represented as “This text is static and will never change”.

“This text is static and will never change” and “Bla bla bla …. more bla bla bla” are of course not the true strings – i replaced them because they are domain specific, not relevant to the problem and they reveal sensitive data that must not be shown!

Again, any help will be highly appreciated. Thanks.

  • 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-09T11:20:17+00:00Added an answer on June 9, 2026 at 11:20 am

    Use:

    /*/tr[2]/td[2]/p/span/text()
    

    When this XPath expression is evaluated against the following XML document (obtained by turning the provided malformed HTML into a wellformed XML document):

    <table>
        <tr>
            <td>
                <p>
                    <span>text</span>
                </p>
            </td>
            <td>
                <p>
                    <span>text</span>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <span>Some text</span>
                </p>
            </td>
            <td>
                <p>
                    <span>text to extract</span>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <span>text</span>
                </p>
            </td>
            <td>
                <p>
                    <span>text</span>
                </p>
            </td>
        </tr>
    </table>
    

    the text node with value "text to extract" is selected, as required.

    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[2]/td[2]/p/span/text()"/>"
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the same XML document (above), the XPath expression is evaluated and the result of this evaluation is copied to the output:

    "text to extract"
    

    Alternatively, if you know the text but want to select an element containing it (say td), then use:

    //text()[. = 'text to extract']/ancestor::td[1]
    

    Again with 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=
          "//text()[. = 'text to extract']/ancestor::td[1]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    The result now is:

    <td>
       <p>
          <span>text to extract</span>
       </p>
    </td>
    

    Still another guess:

    If you want to find the closest preceding text node, then use:

    //text()[. = 'text to extract']/preceding::text()[1]
    

    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=
          "//text()[. = 'text to extract']/preceding::text()[1]"/>"
     </xsl:template>
    </xsl:stylesheet>
    

    Result:

    "Some text"
    

    Update:

    After the latest update by the OP, and his new explanation, the XPath expression he is looking for is:

    //text()[. = 'This text is static and will never change']/following::text()[1]
    

    This selects the text node with string value:

    "Bla bla bla .... more bla bla bla"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an <img> in an HTML document that I would like to highlight
I have an iframe that looks like this: <iframe id=iframe2 ...> #document <html>...</html> </iframe>
I have an HTML document where I would like to semantically group text to
I have a block of HTML that I would like to use as the
I have an html form that I would like to add inputs fields to
I have a document that loads into an iframe. Now I would like to
I have a word document that I would like to have viewed as an
Ok so i have an html document that i reference in my visual studio
I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have an ExtJS application that needs to display an html document within a

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.