Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Xpath

 
Rajan Thiyagarajan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<CCC/>
<CCC/>
<CCC/>
</AAA>
The following XPath will select all middle BBB elements of the above tree structure.
//BBB[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]
Can somebody tell me how ?
Thanks
rajan
 
Micheal Jacob
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajan,
Let see ur xml this way.

so the xpath is //BBB[ position() = floor(last() div 2 + 0.5)]
As it is // it will find all BBB tags.The filter checks whether the element position is matching the other side floor(last() div 2 + 0.5).
from this, last() give you 11,
last() div 2 give you 5.5
last() div 2 + 0.5 give you 6
so it will find the 6 BBB element.
Thats how it arrives at middle element.
Hope this helps you.
Micheal
 
Micheal Jacob
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajan,
Please ignore the above mail,
Use same xml but the answer will be 4 th BBB tag.
Because inside // the context is changed
so last() gives you 8
last() div 2 gives you 4
last() div 2 + 0.5 gives you 4.5
floor(last()...) gives you 4
so it selects the BBB tag whose position is 4.
Micheal
 
Rajan Thiyagarajan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic