質問

Assuming we have such a structure (the number of b in every a is unknown):

<a>
    <b/>
    <b/>
    <b/>
</a>
<a>
    <b/>
    <b/>
    <b/>
</a>

How would we express the following phrase in xpath: "top 4 b elements nested into a"

The a/b[position() <= 4] for obvious reasons returns all 6 elements.

How would I limit it to 4?

I've found that (a/b)[position() <= 4] should work, but seems it's xpath 2.0. Any ideas for 1.0 version?

役に立ちましたか?

解決 2

Why do you say

(a/b)[position() <= 4] should work, but seems it's xpath 2.0

? That's perfectly legitimate XPath 1.0, and in fact is a common idiom for this purpose. I just tested it to confirm that it's accepted and works correctly.

It may also be more efficient than using count(preceding::b), depending on the XPath processor.

他のヒント

Not pretty, but this checks how many <b>s there are earlier in the document.

a/b[count(preceding::b) < 4]

It's not perfect. If there are other <b>s not inside of <a>s it'll fail. For example:

<b>oops</b>
<a>
    <b/>
    <b/>
    <b/>
</a>
<a>
    <b/>
    <b/>
    <b/>
</a>

This one doesn't get tripped up by the <b>oops</b> element.

a/b[count(preceding::b/parent::a) < 4]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top