0

I haven't really found on the internet much examples of these types of XPath and the actual "name" for this type of XPath functionality. Can someone clarify for me, some W3C or other standard online resource that defines these?

(//someXPathThatReturnsMultipleMatches)[n]

where it returns the nth match in the set

//div[span[a[@title='foo']]] 

where we match & return the div (not the hyperlink) which contains a span which in turn contains a hyperlink with title of "foo"

Update: I guess there is no specific name to these XPath examples then? Both the current answers are good, wish I can accept them both though. But first an update to the question:

The basis to asking this question is also a way to easily inform a user some additional types of XPath queries that can not be done in CSS, beyond the more simple examples of accessing parent/ancestor and preceding-siblings and text contains() (which is only a pseudo selector not part of CSS3) that are not available with CSS. Because to those not so well familiar with XPath, and only know basics of CSS, they think you can do most of XPath with CSS, when in fact not (for the complex/unique cases). So I wonder, how would one in essence, summarize effectively what you can not do in CSS that you can with XPath that includes the examples I first mentioned along with the easier examples one would have heard of?

David
  • 3,223
  • 3
  • 29
  • 41

2 Answers2

1

You can check the grammar that defines the XPath language.

I also tried to google for nesting predicates XPath, returns Working With Nested XPath Predicates ... Refined among others.

XPath nth nodelist in Google returns for example How to select specified node within Xpath node sets by index with Selenium?.

Community
  • 1
  • 1
choroba
  • 231,213
  • 25
  • 204
  • 289
1

XPath traverses any document by axis steps. An example for simple child steps, always selecting direct child nodes of the current context with the name of the axis step:

/these/are/all/axis/steps

There are a bunch more kinds of axis steps, which you can look up in pretty much every tutorial, and they're not of importance for this question.

After each step, you can use predicates to further restrict the result set. Predicates filter the results. In the end, it boils down to two kinds of predicates:

  • Numerical predicates return the n-th result of the current step.

    Your first example query (given n is replaced by a number) is an example for this category. As @choroba pointed out, they are actually just abbreviations for (boolean) positional predicates of the form position()=n.

  • Boolean predicates return all results for which the expression evaluates to true. If the result is not boolean anyway (for example as results of functions), non-empty sets are evaluated to be true, empty sets to false. I explained the mechanics behind this in detail in another answer.

    Your second query is an example for this category. It selects <div/> elements, that contain a (span that contain an (anchor tag those title attribute is "foo")). (Read the parens like you would do in math, I added them to clarify precedence.)

Community
  • 1
  • 1
Jens Erat
  • 37,523
  • 16
  • 80
  • 96