S
S
Shimpanze2021-05-08 07:40:10
XPath
Shimpanze, 2021-05-08 07:40:10

How to get the current element AND its first child using "ancestor-or-self"?

Hello!

There is html (the example is synthetic, the real example is much more complicated, that's why I want to use the "ancestor-or-self" axis):

<body>
  <div>
    <p id="foo"></p>
    <p id="bar"> <- получить этот узел...
      <a> <- ...И этот узел
      <a>


Need to get "p#bar" and its first "a".

I tried writing like this:

body/div/p[@id='bar']/node()[descendant-or-self::a[1]]


...does not work...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sundukov, 2021-05-12
@Shimpanze

ancestor-or-self, by definition, cannot do this, because will return the entire set of ancestors. But if you need to " get "p#bar" and its first "a" ", then this:
//p[@id="bar"]/a[1] | //p[@id="bar"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question