3

I am working on Orbeon forms and i have to use Xpath to retrieve attribute value. My scenario 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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Avernet. This one worked. And for second part of my question i used as below and worked fine. (/n/node/page[@id<'6' and .='true'])[last()]/@id
0

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]

1 Comment

Hi, thanks for responding. I dont think postion() < 2 is the right expression here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.