XPath: //B[2] doesn't select anything

T

Timo Nentwig

Hi!

"//B" and "//B[1]" do select all Bs. But //B[2] does not select anything
(should select the second B).

Why?

Timo
 
P

Patrick TJ McPhee

% "//B" and "//B[1]" do select all Bs. But //B[2] does not select anything
% (should select the second B).
%
% Why?

Have to guess here, because you couldn't be bothered to provide a sample
of your input, but I suppose it looks like this

<A>
<C>
<B/>
</C>
<C>
<B/>
</C>
<C>
<B/>
</C>
<C>
<B/>
</C>
</A>

In that case, I'd expect //B to select 4 Bs, //B[1] to also select 4 Bs,
and //B[2] to not select any Bs. [2] is short-hand for [position() = 2],
where position() is the position relative to the node's siblings. There
is no B[2] in the document above.
 
T

Timo Nentwig

Patrick said:
<A>
<C>
<B/>
</C>

or, e.g.:

<C>
<D>
<B/>
</D>
</A>

In that case, I'd expect //B to select 4 Bs, //B[1] to also select 4 Bs,
and //B[2] to not select any Bs. [2] is short-hand for [position() = 2],
where position() is the position relative to the node's siblings. There
is no B[2] in the document above.

Hm, ok. And how do I select the second B of all Bs anyway?

Timo
 
D

Dimitre Novatchev [MVP]

Timo Nentwig said:
In that case, I'd expect //B to select 4 Bs, //B[1] to also select 4 Bs,
and //B[2] to not select any Bs. [2] is short-hand for [position() = 2],
where position() is the position relative to the node's siblings. There
is no B[2] in the document above.

Hm, ok. And how do I select the second B of all Bs anyway?


(//B)[2]


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top