My Snippets

Get last element in an XML element list

XML:
  1. <car>
  2.   <wheel name="frontleft">FL</wheel>
  3.   <wheel name="frontright">FR</wheel>
  4.   <wheel name="backleft">BL</wheel>
  5.   <wheel name="backright">BR</wheel>
  6. </car>

xpath: //wheel[last()=position()]
=>

XML:
  1. <wheel name="backright">BR</wheel>

August 29th, 2008 Posted by | xml | no comments
[]

Position/index of an xml node

XML:
  1. <car>
  2. <wheel name="frontleft">...</wheel>
  3. <wheel name="frontright">...</wheel>
  4. <wheel name="backleft">...</wheel>
  5. <wheel name="backright">..</wheel>
  6. </car>

xpath:
count(//car/wheel[@name='backleft']/preceding-sibling::*) + 1
=> 3

February 10th, 2008 Posted by | xml | no comments
[]