开发者

How to get attribute value based on node value using xpath

I am working on Orbeon forms and i have to use Xpath to retrieve attribute value. My scena开发者_JAVA技巧rio is as mentioned below.

I have a xml node as below.

<n>
<node>
<page id='1'>true</page>
<page id='2'>false</page>
<page id='3'>true</page>
<page id='4'>true</page>
<page id='5'>false</page>
<page id='6'>true</page>
</node>
</n>

Now when i pass any attribute value to xpath, it should return me the attribute value where it finds the next node having value 'true'. For example, if i pass id=1, then i should get the result as 3 since after , the next node having true is . Please note i must get only 3 and not 3,4 and 6.

I have tried something like the below, but i am not getting the expected result.

/n/node/page[@id>"1" and .='true']/@id

Extending my question:

Same case should apply if i give the last value and expect to have to next below attribute value having true. Example. If i give 6, xpath expression should give me 4. I dont mind if i have to use another xpath expression for this.

Please help me.


I gather you would like to get the first page with @id higher than 1 and content equal to true. Then the following will do the trick:

(/n/node/page[@id>"1" and .='true'])[1]/@id/string()

Compared to what you had, picking the "first item that matches…" is done with (…)[1], and I use /string() at the end to get the value of the attribute. Depending on where you are using this expression, this won't be necessary as the attribute will be converted to a string for you.


try using

/n/node/page[@id>"1" and .='true']/@id[position() < 2]

extending my answer:

you can also use

/n/node/page[@id>"1" and .='true']/@id[last() - 1]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜