Ways to choose a distant relative in XML using XPath

To target all direct <p>...</p> elements inside a div, the selector would be

div/p

If I wish to select all second-level nested <p>...</p> tags within a div, I could use

div/*/p

Can a specific range be specified instead?

div/descendant[1-2]::p // retrieve immediate and second-level descendent tags

Answer №1

If you're looking for a simple way to express this, consider using a union operator.

div/p | div/*/p or (div | div/*)/p

There isn't a straightforward method to select a descendant with a range of ancestors, but there might be a workaround depending on the input.

For example:

<root>
   <div>
      <p>good</p>
   </div>
   <div>
      <foo>
         <p>also good</p>
      </foo>
   </div>
   <div>
      <foo>
         <bar>
            <p>this one is not good</p>
         </bar>
      </foo>
   </div>
</root>

You could target specific nodes by using //p[count(./ancestor::*) < 4].

However, this approach may not always be more efficient than utilizing the union operator.

To define a range of nodes between the target and its parent, you can try something like:

//p[count(ancestor::*) - count(ancestor::div[1]/*) < 3]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Styling text on a HTML webpage

Is there a way to center text in the caption and add a link on the far-right of the caption? Let me know if this is possible. The format for the caption should be: Centered Text Right-justified link ...

Browser displaying a CSS error: "Invalid property name" while applying pseudo-element :after

I encountered an issue in Chrome and Explorer while attempting to set a CSS property for a pseudo element :after. (I'm trying to create a styled burger nav icon) The error message I received was: 'Unknown property name' This happened wh ...

Is there a way to restore a minimized min.css file?

Recently, I attempted to utilize an online tool for unminifying my CSS code. However, when I replaced the minified code with the unminified version and accessed the URL, I discovered that the entire website had lost its layout. Unfortunately, relying sole ...

The tab functionality in Python Selenium is effective when using Firefox, however, when using Chrome, it

I have been working on a script that is designed to open multiple websites in new tabs. Initially, everything seemed to work perfectly in Firefox. However, when testing the script on Chrome, I encountered an issue. It opens a new tab as expected but the l ...

Exploring the Features and Functions of Google Chrome Through Selenium

Is it feasible to establish the Chrome homepage using capabilities and Chrome options within Selenium? ...

Error encountered when attempting to utilize a variable within an EJS template document

I seem to be encountering some difficulties while attempting to get an EJS template file to recognize a variable that holds the rows from an SQLite3 table query in a related .js file. Whenever I try to access that route by launching the server, I receive a ...

What could be the reason overflow:hidden isn't functioning properly in IE6?

Would you mind checking this out? http://jsfiddle.net/UQNJA/1/ It displays correctly in all updated browsers, including IE7/8 and 9. However, in IE6, the red and pink borders do not contain the floated <li>s. Any suggestions on how to fix this issu ...

Optimizing Open Graph tags for sharing WhatsApp links

I am utilizing Open Graph tags to optimize sharing on social media platforms. The image I have specified is 1200 x 630 pixels and displays correctly on Facebook as well as Whatsapp: <meta property="og:image" content="https://emotionathlet ...

The practice of extracting data from websites by utilizing the Selenium framework in Python does not involve

My objective is to scrape all the links of products along with their details. Interestingly, when I use a VPN, the product list opens up perfectly fine. However, when I try to access it using a normal browser or proxy, it doesn't work at all. I have a ...

Ways to deactivate a button using CSS

I need to use Javascript to disable a link based on its id. By default, the link is invisible. I will only enable the link when the specific id value comes from the backend. HTML <li id="viewroleId" style="display: none;"> <a href="viewrole" ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

Semi-Transparent Photo Slideshow

Currently, I am in the process of developing a Pokedex-like feature for a project that I am working on. The functionality is working as expected, but there is one particular feature that I would like to implement. My goal is to display only certain element ...

Creating an HTML list based on a hierarchical MySQL table structure

I have retrieved a hierarchical table showing different elements and their parent-child relationships as follows: id| name | parent_id | header 1 | Assets | 0 | Y 2 | Fixed Assets | 1 | Y 3 | Asset One | 2 | N 4 | ...

What is the maximum size allowed for a background image in CSS?

Within a 70 by 50 px box, I have various SVG images with different aspect ratios - some portrait and others landscape. Setting background-size: 70px auto; works for landscape images but distorts the portraits by stretching them vertically. Is there a way t ...

Incorporate fresh Google sites into different web pages using iFrame integration

Wishing you a fantastic day! I am currently working on embedding a brand new Google site into another webpage. I attempted to use an iframe for this purpose, but unfortunately it did not work as expected. Here is the code snippet: <iframe width="1280 ...

Is it possible for me to generate values using PHP that can be easily read by JavaScript?

I'm currently building a website and I am facing some challenges when trying to incorporate JavaScript for real-time calculations. Here are my issues: Is there a more efficient way to avoid manually typing out the code for each level up to 90, lik ...

Can one retrieve the CSS/XPath from a Capybara Element?

Is it possible to extract CSS/XPath from a Capybara Element? I have attempted to use #path but it doesn't appear to be effective. Here is the link I tried: The test is being executed on Selenium Webdriver. ...

Create code with a form button and showcase the produced code on the webpage

I have created two files: code_generator.html, which takes input in the form of an image URL and landing URL. When I click on the submit button, it calls code_created.php. <html> <head> </head> <body> <form a ...

Hover shows no response

I'm having trouble with my hover effect. I want an element to only be visible when hovered over, but it's not working as expected. I've considered replacing the i tag with an a, and have also tried using both display: none and display: bloc ...

selenium AutoIt causing keyboard typing errors

I'm currently using AutoIt to upload Selenium files, but I'm facing an issue where it enters the file path incorrectly during the upload process. https://i.stack.imgur.com/ptGHv.png autoIt = new AutoItX3(); autoIt.WinActivate("Save As" ...