Xpath difference between //B[position() = 1] and /descendant::B[position() = 1]

Viewed 28

Could someone explain to me the difference between these two expressions and give me an example where they are equal and another where they are different? Because for me they are the same

//B[position() = 1]

/descendant::B[position() = 1]
1 Answers

Take

<B>
  <A>A1</A>
  <B>B1</B>
</B>

as the sample input, that way your second expression selects the first B element only while your first one selects both B elements (as it translates into /descendant-or-self::node()/B[1]).

Related