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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:42:08+00:00 2026-05-27T16:42:08+00:00

I have this xPath expression that I’m putting into htmlCleaner: //table[@class=’StandardTable’]/tbody/tr[position()>1]/td[2]/a/img Now, my issue

  • 0

I have this xPath expression that I’m putting into htmlCleaner:

 //table[@class='StandardTable']/tbody/tr[position()>1]/td[2]/a/img

Now, my issue is that it changes, and some times the /a/img element is not present. So I would like an expression that gets all elements

//table[@class='StandardTable']/tbody/tr[position()>1]/td[2]/a/img

when /a/img is present, and

//table[@class='StandardTable']/tbody/tr[position()>1]/td[2]

when /a/img is not present.

Does anyone hav any idea how to do this? I found in another question something that looks like it might help me

descendant-or-self::*[self::body or self::span/parent::body]

but I don’t understand it.

  • 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-27T16:42:08+00:00Added an answer on May 27, 2026 at 4:42 pm

    You can select the union of two mutually exclusive expressions (notice the | union operator):

    //table[@class='StandardTable']/tbody/tr[position()>1]/td[2]/a/img|
    //table[@class='StandardTable']/tbody/tr[position()>1]/td[2][not(a/img)]
    

    When the first expression returns nodes, the second one will not (and the other way around), which means you’ll always get just the required nodes.

    From your comments on @Dimitre’s answer, I see that HTMLCleaner doesn’t fully support XPath 1.0. You don’t really need it to. You just need HTMLCleaner to parse input that isn’t well-formed. Once it has done that job, convert its output into a standard org.w3c.dom.Document and treat it as XML.

    Here’s a conversion example:

    TagNode tagNode = new HtmlCleaner().clean("<html><div><p>test");
    Document doc = new DomSerializer(new CleanerProperties()).createDOM(tagNode);
    

    From here on out, just use JAXP with whatever implementation you want:

    XPath xpath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xpath.evaluate("/html/body/div/p[not(child::*)]", 
                           doc, XPathConstants.NODE);
    System.out.println(node.getTextContent());
    

    Output:

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

Sidebar

Related Questions

I have this xpath expression that does not work //div[child[0]::h4[text()[contains(.,'Dir')]]]/a To parse this html:
I have a xpath expression that returns a list of urls that are in
I have a XPath expression that is supposed to return/get only one node out
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do
I have this class: Class B { private String D; private String E; }
I have an XML file that's structured like this: <foo> <bar></bar> <bar></bar> ... </foo>
I'm trying to get an XPath expression together that will give me all the
I've got the following XPath expression's working in firebug, that returns the correct element(s):
I have this: <div class=down_butt_pad1 style= id=downloadlink><a href=http://www.link.com class=down_butt1 onclick=javascript:window.open('http://s.spam.com','popunder','width=800,height=800,scrollbars=yes,status=no,resizable=yes, toolbar=no'); window.focus();></a></div> I'm trying
I use this xpath expression to list all NON TEST items: /Items/Item[State!='TEST']/Name Normally input

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.