0

I want to select a set of elements as nodes (the content of div[@class="Adres"]):

<div class="KolumnaStyl">
  <div class="Nazwa">ABCD</div>
    <div class="Adres">
      12-345 Warszawa
      <br/>
      ul. Krasnobrodzka 5
      <br/>
      Mazowieckie 

This can be done with:

//div[@class="KolumnaStyl"]/div[@class="Adres"]/node()

As it happens, there are two identical div[@class="Adres"] on the page, which means that node() currently selects the content of both of them. I can't, however, call //div[@class="KolumnaStyl"][1] - that doesn't select the first occurrence.

How can I select a unique set of nodes if the parent directory exists multiple times?

the Tin Man
  • 155,156
  • 41
  • 207
  • 295
Severin
  • 8,258
  • 12
  • 65
  • 112

2 Answers2

0

If you want the first matched one, then do :

(//div[@class="KolumnaStyl"]//div[@class="Adres"])[1]/node()

In XPATH first means 1, not 0.

Here is a sample HTML :

<body>
    <div class="foo">
        <p> 12 </p>
    </div>
    <div class="foo">
        <p> 112 </p>
    </div>
</body>

XPATH expression:

(//div[@class = 'foo'])[1]

output

<div class="foo">
  <p>12</p>
</div>
Arup Rakshit
  • 113,563
  • 27
  • 250
  • 306
0

Take a look at "XPath Get first element of subset".

Basically, predicates have higher precedence than the / and // operators.

So, (//div[@class="KolumnaStyl"]//(div[@class="Adres"]))[1] should return your desired result.

Also check out the spec for more background info.

Community
  • 1
  • 1
Momer
  • 3,062
  • 21
  • 23