• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Xpath

 
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
 
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.
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic